Mode of #cas
integerDigits
The digits of a natural number in any base , with a theorem about Mathlib’s Nat.digits.
#cas [integerDigits] nNat.digits 10 n = [d₀, …, dₖ].#cas [integerDigits] (n, b)Nat.digits b n = [d₀, …, dₖ].integerDigits writes a number in a base, as Mathematica’s IntegerDigits does, with the most significant digit first, the way you would write the number down. The one thing to watch is that the theorem goes the other way. It is about Mathlib’s Nat.digits, which lists the digits least significant first, so its list is the printed one reversed. I think that’s the right trade. The report reads naturally, and the theorem comes in the form that Mathlib’s lemmas about digits expect, such as Nat.ofDigits_digits and Nat.modEq_nine_digits_sum.
Examples (13)#
Basic Examples (2)
The decimal digits of 2026:
#cas [integerDigits] 2026
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
[2, 0, 2, 6]- Domain
ℕ- Input (natural numbers)
2026 in base 10
Checked by Lean’s kernel: digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 10 2026 = [6, 2, 0, 2]
The certificate: 1 checked step
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 10 2026 = [6, 2, 0, 2]
The binary digits of 1234:
#cas [integerDigits] (1234, 2)
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
[1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0]- Domain
ℕ- Input (natural numbers)
1234 in base 2
Checked by Lean’s kernel: digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 2 1234 = [0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1]
The certificate: 1 checked step
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 2 1234 = [0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1]
Scope (3)
Any base from 2 up. In base 16 the digits run from 0 to 15:
#cas [integerDigits] (255, 16)
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
[15, 15]- Domain
ℕ- Input (natural numbers)
255 in base 16
Checked by Lean’s kernel: digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 16 255 = [15, 15]
The certificate: 1 checked step
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 16 255 = [15, 15]
Closed expressions, here , which has ten ones in base 2:
#cas [integerDigits] (2^10 - 1, 2)
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]- Domain
ℕ- Input (natural numbers)
1023 in base 2
Checked by Lean’s kernel: digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 2 1023 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
The certificate: 1 checked step
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 2 1023 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Zero has no digits:
#cas [integerDigits] (0, 10)
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
[]- Domain
ℕ- Input (natural numbers)
0 in base 10
Checked by Lean’s kernel: digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 10 0 = []
The certificate: 1 checked step
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 10 0 = []
Applications (3)
The theorem. Only the three standard axioms are used. Mathlib’s list starts with the last binary digit of 1234:
theorem digits_1234 : Nat.digits 2 1234 = [0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1] := by cas #print axioms digits_1234
digits_1234 depends on the axioms propext, Classical.choice, Quot.sound.
Mathlib’s Nat.ofDigits reads digits back into a number, and Nat.ofDigits_digits turns the theorem into a statement about it:
example : Nat.ofDigits 2 [0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1] = 1234 := by rw [← digits_1234, Nat.ofDigits_digits]
Lean accepts the proof.
Repeated squaring computes from the binary digits of 100: a squaring for each digit after the first, and a multiplication for each of those that is 1, eight steps in all:
#cas [integerDigits] (100, 2)
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
[1, 1, 0, 0, 1, 0, 0]- Domain
ℕ- Input (natural numbers)
100 in base 2
Checked by Lean’s kernel: digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 2 100 = [0, 0, 1, 0, 0, 1, 1]
The certificate: 1 checked step
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 2 100 = [0, 0, 1, 0, 0, 1, 1]
Properties and Relations (1)
The last three digits of are its remainder modulo 1000, which powerMod computes without the other 28 digits:
#cas [integerDigits] (2^100)
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
[1, 2, 6, 7, 6, 5, 0, 6, 0, 0, 2, 2, 8, 2, 2, 9, 4, 0, 1, 4, 9, 6, 7, 0, 3, 2, 0, 5, 3, 7, 6]- Domain
ℕ- Input (natural numbers)
1267650600228229401496703205376 in base 10
Checked by Lean’s kernel: digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 10 1267650600228229401496703205376 = [6, 7, 3, 5, 0, 2, 3, 0, 7, 6, 9, 4, 1, 0, 4, 9, 2, 2, 8, 2, 2, 0, 0, 6, 0, 5, 6, 7, 6, 2, 1]
The certificate: 1 checked step
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 10 1267650600228229401496703205376 = [6, 7, 3, 5, 0, 2, 3, 0, 7, 6, 9, 4, 1, 0, 4, 9, 2, 2, 8, 2, 2, 0, 0, 6, 0, 5, 6, 7, 6, 2, 1]
#cas (powerMod 2 100 1000)
modular power
376- Domain
ℕ- Input (natural numbers)
2^100 mod 1000
Checked by Lean’s kernel: modular power
2 ^ 100 % 1000 = 376
The certificate: 1 checked step
modular power
2 ^ 100 % 1000 = 376
Possible Issues (3)
Mathlib’s list is the printed one reversed, so a statement about the digits of 2026 has the last digit first:
example : Nat.digits 10 2026 = [6, 2, 0, 2] := by cas
Lean accepts the proof.
Base 1 has no positional digits, and the mode refuses it. Mathlib’s Nat.digits 1 n is the list of ones:
#cas [integerDigits] (1234, 1)
Error
integerDigits: the base must be at least 2
The argument is a number or a pair:
#cas [integerDigits] (1234, 2, 10)
Error
integerDigits: expected `n` or `(n, b)`
Neat Examples (1)
The digits of in base 1000 are the binomial coefficients , because none of them reaches 1000 and the expansion of has no carries. binomial gives them one at a time:
#cas [integerDigits] (1001^10, 1000)
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
[1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1]- Domain
ℕ- Input (natural numbers)
1010045120210252210120045010001 in base 1000
Checked by Lean’s kernel: digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 1000 1010045120210252210120045010001 = [1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1]
The certificate: 1 checked step
digits, most significant first (Mathlib's Nat.digits lists them in reverse)
Nat.digits 1000 1010045120210252210120045010001 = [1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1]
#cas (binomial 10 3)
binomial
120- Domain
ℕ- Input (natural numbers)
(10, 3)
Checked by Lean’s kernel: binomial
Nat.choose 10 3 = 120
The certificate: 1 checked step
binomial
Nat.choose 10 3 = 120
See Also
powerMod · binomial · continuedFraction · fromContinuedFraction
