Guide

Number theory

Divisors, congruences, digits and continued fractions, each with a theorem about Mathlib’s definitions.

In number theory the answers quickly get too big to check by hand, and that’s when I’m most tempted to trust a computer algebra system blindly. Here you don’t have to. You can work with divisors, congruences, digits, continued fractions and the usual combinatorial numbers, and each answer is a theorem about Mathlib’s definitions, such as Nat.divisors, Nat.ModEq or Nat.digits. Its proof checks the answer instead of trusting the search that found it.

Divisors and arithmetic functions

  • divisorsThe divisors of a positive integer below 1012 in increasing order, with a theorem about Mathlib’s Nat.divisors.
  • divisorSigmaThe sum σk(n) of the k-th powers of the divisors of n, with a theorem about Mathlib’s ArithmeticFunction.sigma.
  • eulerPhiEuler’s totient φ(n), the number of residues modulo n coprime to n, with a theorem about Mathlib’s Nat.totient.

Modular arithmetic

  • powerModThe remainder of ab modulo m by repeated squaring, for exponents with hundreds of digits, with a theorem in ℕ.
  • modularInverseThe inverse of an integer modulo m≥2 in ZMod m, or a proof that there is none.
  • chineseRemainderAll solutions of a system of congruences x≡ri(modmi), as one residue modulo the lcm, or a proof that there are none.
  • extendedGCDThe gcd of two natural numbers with Bézout coefficients s and t, and a theorem that as+bt=gcd(a,b).
  • jacobiSymbolThe Jacobi symbol (a/n) of an integer and an odd natural number, with a theorem about Mathlib’s jacobiSym.

Digits and continued fractions

  • integerDigitsThe digits of a natural number in any base b≥2, with a theorem about Mathlib’s Nat.digits.
  • continuedFractionThe continued fraction [a0;a1,…,ak] of a rational number, with a theorem that it equals the number.
  • fromContinuedFractionThe rational value of a finite continued fraction [a0;a1,…,ak], with a theorem in ℚ.

Combinatorial numbers

  • binomialThe binomial coefficient (nk) of natural numbers n and k, with a theorem about Mathlib’s Nat.choose.
  • factorialThe factorial n! of a natural number, with a theorem about Mathlib’s Nat.factorial.
  • fibonacciThe Fibonacci number Fn, with F0=0 and F1=1, and a theorem about Mathlib’s Nat.fib.

The Sunzi Suanjing asks for a number with the remainders 2, 3 and 2 on division by 3, 5 and 7. The answer is complete. The theorem says more than that 23 works: the solutions are exactly the numbers 23+105k:

In[1]:=
#cas [chineseRemainder] ([2, 3, 2], [3, 5, 7])
Out[1]=

all solutions

23 mod 105
Domain
ℕ
Input (natural numbers)
x ≡ 2 mod 3, x ≡ 3 mod 5, x ≡ 2 mod 7

Checked by Lean’s kernel: all solutions

23 < 105 ∧ ∀ (y : ℕ), (∀ p ∈ [(3, 2), (5, 3), (7, 2)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 23 [MOD 105]
The certificate: 1 checked step
  1. all solutions

    23 < 105 ∧ ∀ (y : ℕ), (∀ p ∈ [(3, 2), (5, 3), (7, 2)], y ≡ p.2 [MOD p.1]) ↔ y ≡ 23 [MOD 105]

    Checked by Lean’s kernel

A modular power with an exponent of 27 digits. The proof is repeated squaring, so it never builds the full power, and its size grows with the number of digits of the exponent:

In[2]:=
#cas (powerMod 2 123456789123456789123456789 1000000007)
Out[2]=

modular power

612059118
Domain
ℕ
Input (natural numbers)
2^123456789123456789123456789 mod 1000000007

Checked by Lean’s kernel: modular power

2 ^ 123456789123456789123456789 % 1000000007 = 612059118
The certificate: 1 checked step
  1. modular power

    2 ^ 123456789123456789123456789 % 1000000007 = 612059118

    Checked by Lean’s kernel

The divisor commands certify n<1012, eulerPhi counts up to n≤100000, and binomial, factorial and fibonacci compute values of at most 10000 digits. continuedFraction takes rational numbers only, since an irrational number has an infinite expansion.