Skip to content

math.numtheory

whole-number math: divisors, primes, factorials, counting arrangements, and clock (modular) arithmetic

Math on whole numbers: divisors, primes, factorials, counting, and modular arithmetic. Everything works on integers of any size and gives exact answers, so 100 factorial has all 158 digits.

10 mod 12 makes a value that wraps around at 12, like hours on a clock, where 5 hours after 10 o’clock is 3 o’clock. It keeps wrapping through +, -, *, / and **, and shows as 10 (mod 12).

Two mod values are equal when they land on the same spot, which is called being congruent and written ≡. Dividing works whenever the divisor shares no factor with the modulus. The % operator is different, and just gives a plain remainder.

Tags: numbers

  • gcd greatest common divisor: the largest integer that divides all of them evenly
  • lcm least common multiple: the smallest positive integer they all divide evenly
  • is_prime whether n is prime
  • factors the primes that multiply to give n, smallest first, with repeats
  • factorial n! = 1 × 2 × … × n, exact
  • double_factorial n!! = n × (n - 2) × (n - 4) × … down to 1 or 2, exact
  • hyperfactorial 1¹ × 2² × … × nⁿ, exact
  • superfactorial 1! × 2! × … × n!, exact
  • derangements how many ways n items can be shuffled so that none ends up in its original place, exact
  • choose how many ways there are to pick k things from n when order doesn’t matter (the binomial coefficient), exact
  • permutations how many ways there are to pick k things from n when order matters, exact
  • perm the same as permutations
  • combinations every way to pick k of the list’s items when order doesn’t matter, each as a list in the original order
  • mod_pow the remainder of b to the power e, divided by m, worked out without ever computing the huge power
  • mod_inv the number b from 0 to m - 1 for which a × b leaves remainder 1 when divided by m
  • crt the number that leaves remainder rs[0] when divided by ms[0], rs[1] when divided by ms[1], and so on (the Chinese remainder theorem)
  • residue the plain integer a mod value is at, from 0 to its modulus - 1
  • modulus the number a mod value wraps around at
  • rem the remainder of a divided by b, with the same sign as a, as C’s and JavaScript’s % give it
  • fib the nth Fibonacci number, exact
  • next_prime the smallest prime greater than n
  • prev_prime the largest prime less than n
  • divisors every positive divisor of n, smallest first
  • primes the primes up to n, or from a up to b
  • prime_count how many primes are at most n
  • nth_prime the nth prime, so nth_prime(1) is 2
  • digit_sum the sum of n’s digits, in a base (default 10)
  • digital_root add n’s digits again and again until one digit is left, in a base (default 10)
  • bernoulli the Bernoulli number B(n) as an exact fraction, with B(1) = -1/2 and 0 for odd n above 1
  • numerator the numerator of a fraction in lowest terms, or of a whole number itself
  • denominator the denominator of a fraction in lowest terms, or 1 for a whole number
  • frac_part the part of x after the point, keeping its sign: frac_part(-2.25) is -0.25
  • is_even whether n is even
  • is_odd whether n is odd
  • totient Euler’s totient φ(n)
  • egcd the extended gcd of a and b: [g, x, y] with a × x + b × y = g, their gcd
  • iroot the integer k-th root of x, rounded toward zero, and whether it’s exact, as [root, exact]
  • jacobi the Jacobi symbol (a/n) for an odd positive n: 0, 1 or -1
  • legendre the Legendre symbol (a/p) for an odd prime p: 1 if a is a nonzero square mod p, -1 if it isn’t, 0 if p divides a
  • sqrt_mod every x from 0 to p - 1 with x × x leaving remainder a when divided by the prime p, smallest first
  • discrete_log the smallest x from 0 up with g ** x leaving remainder h when divided by the prime p, the discrete logarithm
  • rsa_d the private exponent d of textbook RSA, from the primes p and q and the public exponent e
  • rsa_encrypt textbook RSA encryption, m ** e mod n, with no padding
  • rsa_decrypt textbook RSA decryption, c ** d mod n, as an integer
  • isqrt the integer part of the square root of n, exact at any size
  • cfrac x as a continued fraction, the list [a0, a1, a2, …] meaning a0 + 1/(a1 + 1/(a2 + …))
1
# mod
2
5 mod 75 (mod 7)
Example outputCtrl+Enter to run
1
# + - * / with a mod or an int wrap around
2
(5 mod 7) * 31 (mod 7)
3
# ** with any integer power, and -1 gives the inverse
4
(3 mod 7) ** -15 (mod 7)
5
# == asks whether they land on the same spot
6
(2 mod 7) ** 3 == 1true
7
# n! is the factorial, touching the number
8
5!120
9
# n!! is the double factorial
10
7!!105
Example outputCtrl+Enter to run
gcd(a: int, b: int, *rest: int) -> int / gcd(xs: list) -> int

greatest common divisor: the largest integer that divides all of them evenly

1
gcd(12, 18)6
2
[12, 18, 27].gcd3
Example outputCtrl+Enter to run

See also lcm

lcm(a: int, b: int, *rest: int) -> int / lcm(xs: list) -> int

least common multiple: the smallest positive integer they all divide evenly

1
lcm(4, 6)12
2
(1..=20).lcm232792560
Example outputCtrl+Enter to run

See also gcd

is_prime(n: int) -> bool

whether n is prime

Certain for n below about 3 × 10²⁴. Above that, a wrong answer is possible but astronomically unlikely (Miller-Rabin test)

1
is_prime(97)true
2
is_prime(2 ** 61 - 1)true
Example outputCtrl+Enter to run

See also factors

factors(n: int) -> list

the primes that multiply to give n, smallest first, with repeats

1
360.factors[2, 2, 2, 3, 3, 5]
2
factors(2 ** 32 + 1)[641, 6700417]
Example outputCtrl+Enter to run

See also is_prime, gcd

factorial(n: int) -> int

n! = 1 × 2 × … × n, exact

Also written n!

1
factorial(5)120
2
30!265252859812191058636308480000000
Example outputCtrl+Enter to run

See also choose, double_factorial

double_factorial(n: int) -> int

n!! = n × (n - 2) × (n - 4) × … down to 1 or 2, exact

Also written n!!

1
double_factorial(7)105
2
8!!384
Example outputCtrl+Enter to run

See also factorial

hyperfactorial(n: int) -> int

1¹ × 2² × … × nⁿ, exact

1
hyperfactorial(4)27648
Example outputCtrl+Enter to run

See also superfactorial, factorial

superfactorial(n: int) -> int

1! × 2! × … × n!, exact

1
superfactorial(4)288
Example outputCtrl+Enter to run

See also hyperfactorial, factorial

derangements(n: int) -> int

how many ways n items can be shuffled so that none ends up in its original place, exact

1
derangements(4)9
Example outputCtrl+Enter to run

See also permutations, factorial

choose(n: num, k: int) -> int|frac|float

how many ways there are to pick k things from n when order doesn’t matter (the binomial coefficient), exact

Also called nCr.

n can also be a fraction or negative, for the general formula used in series expansions

1
choose(5, 2)10
2
choose(52, 5)2598960
3
choose(1/2, 2)-1/8
4
nCr(5, 2)10
5
choose(-1, 3)-1
Example outputCtrl+Enter to run

See also permutations, combinations, factorial

permutations(n: int, k: int) -> int / permutations(xs: list, k?: int) -> list

how many ways there are to pick k things from n when order matters, exact

Also called nPr.

Given a list instead, every ordering of k of its items (all of them by default)

1
permutations(5, 2)20
2
nPr(5, 2)20
3
[1, 2, 3].permutations[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
4
"abc".chars.permutations(2).len6
Example outputCtrl+Enter to run

See also choose, combinations

perm(n: int, k: int) -> int / perm(xs: list, k?: int) -> list

the same as permutations

1
perm(5, 2)20
Example outputCtrl+Enter to run

See also permutations

combinations(xs: list, k: int) -> list

every way to pick k of the list’s items when order doesn’t matter, each as a list in the original order

1
[1, 2, 3, 4].combinations(2)[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
Example outputCtrl+Enter to run

See also choose, permutations

mod_pow(b: int, e: int, m: int) -> int

the remainder of b to the power e, divided by m, worked out without ever computing the huge power

A negative e uses b’s inverse, as in mod_inv

1
mod_pow(2, 100, 7)2
2
mod_pow(3, 10 ** 18, 1000000007)246336683
3
mod_pow(3, -1, 7)5
Example outputCtrl+Enter to run

See also mod_inv, gcd

mod_inv(a: int, m: int) -> int / mod_inv(x: mod) -> mod

the number b from 0 to m - 1 for which a × b leaves remainder 1 when divided by m

An error if a and m share a factor, since then there isn’t one

1
mod_inv(3, 7)5
2
mod_inv(5 mod 12)5 (mod 12)
Example outputCtrl+Enter to run

See also mod_pow, crt

crt(rs: list, ms: list) -> mod / crt(xs: list) -> mod

the number that leaves remainder rs[0] when divided by ms[0], rs[1] when divided by ms[1], and so on (the Chinese remainder theorem)

The answer is a mod, since adding the moduli’s least common multiple gives another. An error if there’s none

1
crt([2, 3, 2], [3, 5, 7])23 (mod 105)
2
crt([2 mod 3, 3 mod 5])8 (mod 15)
Example outputCtrl+Enter to run

See also mod_inv

residue(x: mod) -> int

the plain integer a mod value is at, from 0 to its modulus - 1

1
(17 mod 5).residue2
Example outputCtrl+Enter to run

See also modulus

modulus(x: mod) -> int

the number a mod value wraps around at

1
(17 mod 5).modulus5
Example outputCtrl+Enter to run

See also residue

rem(a: int, b: int) -> int / rem(a: num, b: num) -> float

the remainder of a divided by b, with the same sign as a, as C’s and JavaScript’s % give it

zil’s own % is never negative

1
rem(-7, 3)-1
2
-7 % 32
Example outputCtrl+Enter to run

See also mod_pow

fib(n: int) -> int

the nth Fibonacci number, exact

0, 1, 1, 2, 3, 5, 8, …, each the sum of the two before, starting from fib(0) = 0

1
fib(10)55
2
fib(100)354224848179261915075
Example outputCtrl+Enter to run

See also factorial

next_prime(n: int) -> int

the smallest prime greater than n

1
next_prime(100)101
2
next_prime(2 ** 64)18446744073709551629
Example outputCtrl+Enter to run

See also is_prime

prev_prime(n: int) -> int

the largest prime less than n

1
prev_prime(100)97
Example outputCtrl+Enter to run

See also next_prime, is_prime

divisors(n: int) -> list

every positive divisor of n, smallest first

1
divisors(12)[1, 2, 3, 4, 6, 12]
2
divisors(97)[1, 97]
Example outputCtrl+Enter to run

See also factors, gcd

primes(a: int, b?: int) -> list

the primes up to n, or from a up to b

1
primes(30)[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
2
primes(100, 130)[101, 103, 107, 109, 113, 127]
Example outputCtrl+Enter to run

See also is_prime, prime_count

prime_count(n: int) -> int

how many primes are at most n

1
prime_count(100)25
2
prime_count(1000000)78498
Example outputCtrl+Enter to run

See also primes, nth_prime

nth_prime(n: int) -> int

the nth prime, so nth_prime(1) is 2

1
nth_prime(1)2
2
nth_prime(1000)7919
Example outputCtrl+Enter to run

See also prime_count, next_prime

digit_sum(n: int, base?: int) -> int

the sum of n’s digits, in a base (default 10)

1
digit_sum(2 ** 100)115
2
digit_sum(255, 16)30
Example outputCtrl+Enter to run

See also digital_root

digital_root(n: int, base?: int) -> int

add n’s digits again and again until one digit is left, in a base (default 10)

1
digital_root(9875)2
Example outputCtrl+Enter to run

See also digit_sum

bernoulli(n: int) -> frac

the Bernoulli number B(n) as an exact fraction, with B(1) = -1/2 and 0 for odd n above 1

1
bernoulli(2)1/6
2
bernoulli(12)-691/2730
Example outputCtrl+Enter to run

See also zeta, factorial

numerator(x: num) -> int

the numerator of a fraction in lowest terms, or of a whole number itself

1
numerator(6/8)3
2
numerator(frac(0.75))3
Example outputCtrl+Enter to run

See also denominator

denominator(x: num) -> int

the denominator of a fraction in lowest terms, or 1 for a whole number

1
denominator(6/8)4
2
denominator(5)1
Example outputCtrl+Enter to run

See also numerator

frac_part(x: num) -> float

the part of x after the point, keeping its sign: frac_part(-2.25) is -0.25

1
frac_part(3.75)0.75
2
frac_part(-2.25)-0.25
Example outputCtrl+Enter to run

See also floor, round

is_even(n: int) -> bool

whether n is even

1
is_even(10)true
Example outputCtrl+Enter to run

See also is_odd

is_odd(n: int) -> bool

whether n is odd

1
is_odd(7)true
Example outputCtrl+Enter to run

See also is_even

totient(n: int) -> int

Euler’s totient φ(n)

How many of the numbers from 1 to n share no factor with n

1
totient(36)12
2
totient(97)96
Example outputCtrl+Enter to run

See also factors, gcd

egcd(a: int, b: int) -> list

the extended gcd of a and b: [g, x, y] with a × x + b × y = g, their gcd

x is the inverse of a mod b when g is 1, which is how mod_inv works

1
egcd(240, 46)[2, -9, 47]
2
egcd(3, 7)[1, -2, 1]
Example outputCtrl+Enter to run

See also gcd, mod_inv

iroot(x: int, k: int) -> list

the integer k-th root of x, rounded toward zero, and whether it’s exact, as [root, exact]

Exact at any size, unlike x ** (1/k), which goes through floats. With a small RSA exponent and no padding, iroot(c, e) recovers a message whose power never wrapped past n

1
iroot(1000, 3)[10, true]
2
iroot(10 ** 60 + 1, 3)[100000000000000000000, false]
3
iroot(-27, 3)[-3, true]
Example outputCtrl+Enter to run

See also isqrt, sqrt_mod

jacobi(a: int, n: int) -> int

the Jacobi symbol (a/n) for an odd positive n: 0, 1 or -1

For a prime n it’s the Legendre symbol. -1 means a is certainly not a square mod n, while 1 only means it might be unless n is prime

1
jacobi(2, 15)1
2
jacobi(5, 21)1
Example outputCtrl+Enter to run

See also legendre, sqrt_mod

legendre(a: int, p: int) -> int

the Legendre symbol (a/p) for an odd prime p: 1 if a is a nonzero square mod p, -1 if it isn’t, 0 if p divides a

1
legendre(2, 7)1
2
legendre(3, 7)-1
Example outputCtrl+Enter to run

See also jacobi, sqrt_mod

sqrt_mod(a: int, p: int) -> list

every x from 0 to p - 1 with x × x leaving remainder a when divided by the prime p, smallest first

Empty when a isn’t a square mod p. Uses the Tonelli-Shanks algorithm, so it’s fast for primes of any size

1
sqrt_mod(2, 7)[3, 4]
2
sqrt_mod(3, 7)[]
3
sqrt_mod(10, 13)[6, 7]
Example outputCtrl+Enter to run

See also legendre, isqrt, mod_pow

discrete_log(g: int, h: int, p: int) -> int

the smallest x from 0 up with g ** x leaving remainder h when divided by the prime p, the discrete logarithm

Fast when p - 1 has only small prime factors (Pohlig-Hellman). Otherwise it takes about the square root of the largest one in time and memory (baby-step giant-step), so it stops with an error past the session’s size limit. An error when there’s no such x

1
discrete_log(3, 13, 17)4
2
discrete_log(3, mod_pow(3, 123456789, 998244353), 998244353)123456789
Example outputCtrl+Enter to run

See also mod_pow, factors

rsa_d(p: int, q: int, e: int) -> int

the private exponent d of textbook RSA, from the primes p and q and the public exponent e

d is e’s inverse mod (p - 1) × (q - 1), so rsa_decrypt(c, d, p * q) undoes rsa_encrypt(m, e, p * q)

1
rsa_d(61, 53, 17)2753
Example outputCtrl+Enter to run

See also rsa_decrypt, factors, mod_inv

rsa_encrypt(m: int|bytes, e: int, n: int) -> int

textbook RSA encryption, m ** e mod n, with no padding

m is an integer or bytes, which are read big-endian as int(m) does, and it must be less than n. Without padding the same message always encrypts the same way, so this is for puzzles and learning, never real secrets

1
rsa_encrypt(65, 17, 3233)2790
2
rsa_encrypt(b"hi", 65537, 1000003 * 1000033)259390290167
Example outputCtrl+Enter to run

See also rsa_decrypt, rsa_d, mod_pow

rsa_decrypt(c: int, d: int, n: int) -> int

textbook RSA decryption, c ** d mod n, as an integer

bytes(rsa_decrypt(c, d, n)) turns it back into the message’s bytes

1
rsa_decrypt(2790, 2753, 3233)65
Example outputCtrl+Enter to run

See also rsa_encrypt, rsa_d, bytes

isqrt(n: int) -> int

the integer part of the square root of n, exact at any size

1
isqrt(99)9
2
isqrt(10 ** 40)100000000000000000000
Example outputCtrl+Enter to run

See also sqrt

cfrac(x: num, terms: int = 20) -> list

x as a continued fraction, the list [a0, a1, a2, …] meaning a0 + 1/(a1 + 1/(a2 + …))

Cutting it short gives the best fractions close to x. Exact for fractions, while floats give up to terms numbers

1
(415/93).cfrac[4, 2, 6, 7]
2
pi.cfrac(5)[3, 7, 15, 1, 292]
3
sqrt(2).cfrac(6)[1, 2, 2, 2, 2, 2]
Example outputCtrl+Enter to run

See also frac

1
# prime factors
2
factors(2 ** 32 + 1)[641, 6700417]
3
# is 2 to the 61st minus 1 prime?
4
is_prime(2 ** 61 - 1)true
5
# how many 5-card poker hands there are
6
choose(52, 5)2598960
7
# podium finishes from 8 runners
8
permutations(8, 3)336
9
# every way to pick 2 of them
10
[1, 2, 3].combinations(2)[[1, 2], [1, 3], [2, 3]]
11
# the remainder of 3 to the 1000th, divided by 7
12
mod_pow(3, 1000, 7)4
13
# 5 hours after 10 o'clock
14
10 mod 12 + 53 (mod 12)
15
# what times 3 gives 1, wrapping at 7?
16
1 / (3 mod 7)5 (mod 7)
17
# x with x % 3 == 2 and x % 5 == 3
18
crt([2, 3], [3, 5])8 (mod 15)
19
# the 1000th Fibonacci number has 209 digits
20
fib(1000).str.len209
21
# the start of pi's continued fraction
22
pi.cfrac(4)[3, 7, 15, 1]
23
# the next prime after a googol
24
(10 ** 100).next_prime - 10 ** 100267
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.