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.
writing
Section titled “writing”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).
good to know
Section titled “good to know”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
gcdgreatest common divisor: the largest integer that divides all of them evenlylcmleast common multiple: the smallest positive integer they all divide evenlyis_primewhether n is primefactorsthe primes that multiply to give n, smallest first, with repeatsfactorialn! = 1 × 2 × … × n, exactdouble_factorialn!! = n × (n - 2) × (n - 4) × … down to 1 or 2, exacthyperfactorial1¹ × 2² × … × nⁿ, exactsuperfactorial1! × 2! × … × n!, exactderangementshow many ways n items can be shuffled so that none ends up in its original place, exactchoosehow many ways there are to pick k things from n when order doesn’t matter (the binomial coefficient), exactpermutationshow many ways there are to pick k things from n when order matters, exactpermthe same as permutationscombinationsevery way to pick k of the list’s items when order doesn’t matter, each as a list in the original ordermod_powthe remainder of b to the power e, divided by m, worked out without ever computing the huge powermod_invthe number b from 0 to m - 1 for which a × b leaves remainder 1 when divided by mcrtthe 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)residuethe plain integer a mod value is at, from 0 to its modulus - 1modulusthe number a mod value wraps around atremthe remainder of a divided by b, with the same sign as a, as C’s and JavaScript’s%give itfibthe nth Fibonacci number, exactnext_primethe smallest prime greater than nprev_primethe largest prime less than ndivisorsevery positive divisor of n, smallest firstprimesthe primes up to n, or from a up to bprime_counthow many primes are at most nnth_primethe nth prime, so nth_prime(1) is 2digit_sumthe sum of n’s digits, in a base (default 10)digital_rootadd n’s digits again and again until one digit is left, in a base (default 10)bernoullithe Bernoulli number B(n) as an exact fraction, with B(1) = -1/2 and 0 for odd n above 1numeratorthe numerator of a fraction in lowest terms, or of a whole number itselfdenominatorthe denominator of a fraction in lowest terms, or 1 for a whole numberfrac_partthe part of x after the point, keeping its sign: frac_part(-2.25) is -0.25is_evenwhether n is evenis_oddwhether n is oddtotientEuler’s totient φ(n)egcdthe extended gcd of a and b: [g, x, y] with a × x + b × y = g, their gcdirootthe integer k-th root of x, rounded toward zero, and whether it’s exact, as [root, exact]jacobithe Jacobi symbol (a/n) for an odd positive n: 0, 1 or -1legendrethe 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 asqrt_modevery x from 0 to p - 1 with x × x leaving remainder a when divided by the prime p, smallest firstdiscrete_logthe smallest x from 0 up with g ** x leaving remainder h when divided by the prime p, the discrete logarithmrsa_dthe private exponent d of textbook RSA, from the primes p and q and the public exponent ersa_encrypttextbook RSA encryption, m ** e mod n, with no paddingrsa_decrypttextbook RSA decryption, c ** d mod n, as an integerisqrtthe integer part of the square root of n, exact at any sizecfracx as a continued fraction, the list [a0, a1, a2, …] meaning a0 + 1/(a1 + 1/(a2 + …))
1# mod25 mod 75 (mod 7)syntax
Section titled “syntax”1# + - * / with a mod or an int wrap around2(5 mod 7) * 31 (mod 7)3# ** with any integer power, and -1 gives the inverse4(3 mod 7) ** -15 (mod 7)5# == asks whether they land on the same spot6(2 mod 7) ** 3 == 1true7# n! is the factorial, touching the number85!1209# n!! is the double factorial107!!105Functions
Section titled “Functions”gcd(a: int, b: int, *rest: int) -> int / gcd(xs: list) -> int
greatest common divisor: the largest integer that divides all of them evenly
1gcd(12, 18)62[12, 18, 27].gcd3See 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
1lcm(4, 6)122(1..=20).lcm232792560See also gcd
is_prime
Section titled “is_prime”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)
1is_prime(97)true2is_prime(2 ** 61 - 1)trueSee also factors
factors
Section titled “factors”factors(n: int) -> list
the primes that multiply to give n, smallest first, with repeats
1360.factors[2, 2, 2, 3, 3, 5]2factors(2 ** 32 + 1)[641, 6700417]factorial
Section titled “factorial”factorial(n: int) -> int
n! = 1 × 2 × … × n, exact
Also written n!
1factorial(5)120230!265252859812191058636308480000000See also choose, double_factorial
double_factorial
Section titled “double_factorial”double_factorial(n: int) -> int
n!! = n × (n - 2) × (n - 4) × … down to 1 or 2, exact
Also written n!!
1double_factorial(7)10528!!384See also factorial
hyperfactorial
Section titled “hyperfactorial”hyperfactorial(n: int) -> int
1¹ × 2² × … × nⁿ, exact
1hyperfactorial(4)27648See also superfactorial, factorial
superfactorial
Section titled “superfactorial”superfactorial(n: int) -> int
1! × 2! × … × n!, exact
1superfactorial(4)288See also hyperfactorial, factorial
derangements
Section titled “derangements”derangements(n: int) -> int
how many ways n items can be shuffled so that none ends up in its original place, exact
1derangements(4)9See also permutations, factorial
choose
Section titled “choose”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
1choose(5, 2)102choose(52, 5)25989603choose(1/2, 2)-1/84nCr(5, 2)105choose(-1, 3)-1See also permutations, combinations, factorial
permutations
Section titled “permutations”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)
1permutations(5, 2)202nPr(5, 2)203[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).len6See also choose, combinations
perm(n: int, k: int) -> int / perm(xs: list, k?: int) -> list
the same as permutations
1perm(5, 2)20See also permutations
combinations
Section titled “combinations”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]]See also choose, permutations
mod_pow
Section titled “mod_pow”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
1mod_pow(2, 100, 7)22mod_pow(3, 10 ** 18, 1000000007)2463366833mod_pow(3, -1, 7)5mod_inv
Section titled “mod_inv”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
1mod_inv(3, 7)52mod_inv(5 mod 12)5 (mod 12)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
1crt([2, 3, 2], [3, 5, 7])23 (mod 105)2crt([2 mod 3, 3 mod 5])8 (mod 15)See also mod_inv
residue
Section titled “residue”residue(x: mod) -> int
the plain integer a mod value is at, from 0 to its modulus - 1
1(17 mod 5).residue2See also modulus
modulus
Section titled “modulus”modulus(x: mod) -> int
the number a mod value wraps around at
1(17 mod 5).modulus5See 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
1rem(-7, 3)-12-7 % 32See 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
1fib(10)552fib(100)354224848179261915075See also factorial
next_prime
Section titled “next_prime”next_prime(n: int) -> int
the smallest prime greater than n
1next_prime(100)1012next_prime(2 ** 64)18446744073709551629See also is_prime
prev_prime
Section titled “prev_prime”prev_prime(n: int) -> int
the largest prime less than n
1prev_prime(100)97See also next_prime, is_prime
divisors
Section titled “divisors”divisors(n: int) -> list
every positive divisor of n, smallest first
1divisors(12)[1, 2, 3, 4, 6, 12]2divisors(97)[1, 97]primes
Section titled “primes”primes(a: int, b?: int) -> list
the primes up to n, or from a up to b
1primes(30)[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]2primes(100, 130)[101, 103, 107, 109, 113, 127]See also is_prime, prime_count
prime_count
Section titled “prime_count”prime_count(n: int) -> int
how many primes are at most n
1prime_count(100)252prime_count(1000000)78498nth_prime
Section titled “nth_prime”nth_prime(n: int) -> int
the nth prime, so nth_prime(1) is 2
1nth_prime(1)22nth_prime(1000)7919See also prime_count, next_prime
digit_sum
Section titled “digit_sum”digit_sum(n: int, base?: int) -> int
the sum of n’s digits, in a base (default 10)
1digit_sum(2 ** 100)1152digit_sum(255, 16)30See also digital_root
digital_root
Section titled “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)
1digital_root(9875)2See also digit_sum
bernoulli
Section titled “bernoulli”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
1bernoulli(2)1/62bernoulli(12)-691/2730numerator
Section titled “numerator”numerator(x: num) -> int
the numerator of a fraction in lowest terms, or of a whole number itself
1numerator(6/8)32numerator(frac(0.75))3See also denominator
denominator
Section titled “denominator”denominator(x: num) -> int
the denominator of a fraction in lowest terms, or 1 for a whole number
1denominator(6/8)42denominator(5)1See also numerator
frac_part
Section titled “frac_part”frac_part(x: num) -> float
the part of x after the point, keeping its sign: frac_part(-2.25) is -0.25
1frac_part(3.75)0.752frac_part(-2.25)-0.25is_even
Section titled “is_even”is_even(n: int) -> bool
whether n is even
1is_even(10)trueSee also is_odd
is_odd
Section titled “is_odd”is_odd(n: int) -> bool
whether n is odd
1is_odd(7)trueSee also is_even
totient
Section titled “totient”totient(n: int) -> int
Euler’s totient φ(n)
How many of the numbers from 1 to n share no factor with n
1totient(36)122totient(97)96egcd(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
1egcd(240, 46)[2, -9, 47]2egcd(3, 7)[1, -2, 1]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
1iroot(1000, 3)[10, true]2iroot(10 ** 60 + 1, 3)[100000000000000000000, false]3iroot(-27, 3)[-3, true]jacobi
Section titled “jacobi”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
1jacobi(2, 15)12jacobi(5, 21)1legendre
Section titled “legendre”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
1legendre(2, 7)12legendre(3, 7)-1sqrt_mod
Section titled “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
1sqrt_mod(2, 7)[3, 4]2sqrt_mod(3, 7)[]3sqrt_mod(10, 13)[6, 7]See also legendre, isqrt, mod_pow
discrete_log
Section titled “discrete_log”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
1discrete_log(3, 13, 17)42discrete_log(3, mod_pow(3, 123456789, 998244353), 998244353)123456789rsa_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)
1rsa_d(61, 53, 17)2753See also rsa_decrypt, factors, mod_inv
rsa_encrypt
Section titled “rsa_encrypt”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
1rsa_encrypt(65, 17, 3233)27902rsa_encrypt(b"hi", 65537, 1000003 * 1000033)259390290167See also rsa_decrypt, rsa_d, mod_pow
rsa_decrypt
Section titled “rsa_decrypt”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
1rsa_decrypt(2790, 2753, 3233)65See also rsa_encrypt, rsa_d, bytes
isqrt(n: int) -> int
the integer part of the square root of n, exact at any size
1isqrt(99)92isqrt(10 ** 40)100000000000000000000See 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]2pi.cfrac(5)[3, 7, 15, 1, 292]3sqrt(2).cfrac(6)[1, 2, 2, 2, 2, 2]See also frac
More examples
Section titled “More examples”numtheory
Section titled “numtheory”1# prime factors2factors(2 ** 32 + 1)[641, 6700417]3# is 2 to the 61st minus 1 prime?4is_prime(2 ** 61 - 1)true5# how many 5-card poker hands there are6choose(52, 5)25989607# podium finishes from 8 runners8permutations(8, 3)3369# every way to pick 2 of them10[1, 2, 3].combinations(2)[[1, 2], [1, 3], [2, 3]]11# the remainder of 3 to the 1000th, divided by 712mod_pow(3, 1000, 7)413# 5 hours after 10 o'clock1410 mod 12 + 53 (mod 12)15# what times 3 gives 1, wrapping at 7?161 / (3 mod 7)5 (mod 7)17# x with x % 3 == 2 and x % 5 == 318crt([2, 3], [3, 5])8 (mod 15)19# the 1000th Fibonacci number has 209 digits20fib(1000).str.len20921# the start of pi's continued fraction22pi.cfrac(4)[3, 7, 15, 1]23# the next prime after a googol24(10 ** 100).next_prime - 10 ** 100267Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.