Mode of #cas

integerDigits

The digits of a natural number in any base b≥2, with a theorem about Mathlib’s Nat.digits.

#cas [integerDigits] n
lists the decimal digits of the natural number n, most significant first, and proves Nat.digits 10 n = [d₀, …, dₖ].
#cas [integerDigits] (n, b)
lists the digits of n in the base b ≥ 2, most significant first, and proves 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.

Details and Options

  • n and b are natural numbers: literals, or closed integer expressions such as 2^100. Without b the base is 10.
  • The theorem is Nat.digits b n = [d₀, d₁, …, dₖ], with the least significant digit d0 first, and the report prints the reverse [dₖ, …, d₁, d₀]. Mathlib’s tactic norm_num proves the equation, with the tactics decide +kernel and simp as fallbacks.
  • A digit in base b is a natural number below b, so the digits of 255 in base 16 are [15, 15].
  • The digits of 0 form the empty list, since Nat.digits b 0 = [].
  • There are no options.

Examples (13)#

Basic Examples (2)

The decimal digits of 2026:

In[1]:=
#cas [integerDigits] 2026
Out[1]=

digits, most significant first (Mathlib's Nat.digits lists them in reverse)

[2, 0, 2, 6]
[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
  1. digits, most significant first (Mathlib's Nat.digits lists them in reverse)

    Nat.digits 10 2026 = [6, 2, 0, 2]

    Checked by Lean’s kernel


The binary digits of 1234:

In[2]:=
#cas [integerDigits] (1234, 2)
Out[2]=

digits, most significant first (Mathlib's Nat.digits lists them in reverse)

[1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0]
[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
  1. 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]

    Checked by Lean’s kernel

Scope (3)

Any base from 2 up. In base 16 the digits run from 0 to 15:

In[3]:=
#cas [integerDigits] (255, 16)
Out[3]=

digits, most significant first (Mathlib's Nat.digits lists them in reverse)

[15, 15]
[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
  1. digits, most significant first (Mathlib's Nat.digits lists them in reverse)

    Nat.digits 16 255 = [15, 15]

    Checked by Lean’s kernel


Closed expressions, here 210−1, which has ten ones in base 2:

In[4]:=
#cas [integerDigits] (2^10 - 1, 2)
Out[4]=

digits, most significant first (Mathlib's Nat.digits lists them in reverse)

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[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
  1. 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]

    Checked by Lean’s kernel


Zero has no digits:

In[5]:=
#cas [integerDigits] (0, 10)
Out[5]=

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
  1. digits, most significant first (Mathlib's Nat.digits lists them in reverse)

    Nat.digits 10 0 = []

    Checked by Lean’s kernel

Applications (3)

The theorem. Only the three standard axioms are used. Mathlib’s list starts with the last binary digit of 1234:

In[6]:=
theorem digits_1234 : Nat.digits 2 1234 = [0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1] := by
  cas

#print axioms digits_1234
Out[6]=

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:

In[7]:=
example : Nat.ofDigits 2 [0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1] = 1234 := by
  rw [← digits_1234, Nat.ofDigits_digits]
Out[7]=

Lean accepts the proof.


Repeated squaring computes a100 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:

In[8]:=
#cas [integerDigits] (100, 2)
Out[8]=

digits, most significant first (Mathlib's Nat.digits lists them in reverse)

[1, 1, 0, 0, 1, 0, 0]
[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
  1. digits, most significant first (Mathlib's Nat.digits lists them in reverse)

    Nat.digits 2 100 = [0, 0, 1, 0, 0, 1, 1]

    Checked by Lean’s kernel

Properties and Relations (1)

The last three digits of 2100 are its remainder modulo 1000, which powerMod computes without the other 28 digits:

In[9]:=
#cas [integerDigits] (2^100)
Out[9]=

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]
[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
  1. 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]

    Checked by Lean’s kernel

In[10]:=
#cas (powerMod 2 100 1000)
Out[10]=

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
  1. modular power

    2 ^ 100 % 1000 = 376

    Checked by Lean’s kernel

Possible Issues (3)

Mathlib’s list is the printed one reversed, so a statement about the digits of 2026 has the last digit first:

In[11]:=
example : Nat.digits 10 2026 = [6, 2, 0, 2] := by
  cas
Out[11]=

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 n ones:

In[12]:=
#cas [integerDigits] (1234, 1)
Out[12]=

Error

integerDigits: the base must be at least 2

The argument is a number or a pair:

In[13]:=
#cas [integerDigits] (1234, 2, 10)
Out[13]=

Error

integerDigits: expected `n` or `(n, b)`

Neat Examples (1)

The digits of 100110 in base 1000 are the binomial coefficients (10k), because none of them reaches 1000 and the expansion of (1000+1)10 has no carries. binomial gives them one at a time:

In[14]:=
#cas [integerDigits] (1001^10, 1000)
Out[14]=

digits, most significant first (Mathlib's Nat.digits lists them in reverse)

[1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1]
[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
  1. 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]

    Checked by Lean’s kernel

In[15]:=
#cas (binomial 10 3)
Out[15]=

binomial

120
Domain
ℕ
Input (natural numbers)
(10, 3)

Checked by Lean’s kernel: binomial

Nat.choose 10 3 = 120
The certificate: 1 checked step
  1. binomial

    Nat.choose 10 3 = 120

    Checked by Lean’s kernel

See Also

powerMod · binomial · continuedFraction · fromContinuedFraction