Mathlib.Data.Nat.Digits.Defs
74 declarations. Mathlib docs · Source
Digits of a natural number
This provides a basic API for extracting the digits of a natural number in a given base, and reconstructing numbers from their digits.
We also prove some divisibility tests based on digits, in particular completing Theorem #85 from https://www.cs.ru.nl/~freek/100/.
Also included is a bound on the length of Nat.toDigits from core.
TODO
A basic norm_digits tactic for proving goals of the form Nat.digits a b = l where a and b
are numerals is not yet ported.
Properties
This section contains various lemmas of properties relating to digits and ofDigits.
Nat.toDigits length
theorem Nat.digitsAux0.match_1.congr_eq_2 E1 #
∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : (n : ℕ) → motive n.succ) (n : ℕ),
x = n.succ →
(match x with
| 0 => h_1 ()
| n.succ => h_2 n) ≍
h_2 n
theorem Nat.digits.match_1.congr_eq_3 E1 #
∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : Unit → motive 1) (h_3 : (b : ℕ) → motive b.succ.succ)
(b : ℕ),
x = b.succ.succ →
(match x with
| 0 => h_1 ()
| 1 => h_2 ()
| b.succ.succ => h_3 b) ≍
h_3 b
theorem Nat.digits.match_1.congr_eq_1 E1 #
∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : Unit → motive 1) (h_3 : (b : ℕ) → motive b.succ.succ),
x = 0 →
(match x with
| 0 => h_1 ()
| 1 => h_2 ()
| b.succ.succ => h_3 b) ≍
h_1 ()
theorem Nat.digits.match_1.congr_eq_2 E1 #
∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : Unit → motive 1) (h_3 : (b : ℕ) → motive b.succ.succ),
x = 1 →
(match x with
| 0 => h_1 ()
| 1 => h_2 ()
| b.succ.succ => h_3 b) ≍
h_2 ()
theorem Nat.ofDigits.match_1.congr_eq_1 E1 #
∀ (motive : List ℕ → Sort u_1) (x : List ℕ) (h_1 : Unit → motive []) (h_2 : (h : ℕ) → (t : List ℕ) → motive (h :: t)),
x = [] →
(match x with
| [] => h_1 ()
| h :: t => h_2 h t) ≍
h_1 ()
theorem Nat.digitsAux.congr_simp E1 #
∀ (b b_1 : ℕ) (e_b : b = b_1) (h : 2 ≤ b) (a a_1 : ℕ), a = a_1 → b.digitsAux h a = b_1.digitsAux ⋯ a_1
theorem Nat.ofDigits.match_1.congr_eq_2 E1 #
∀ (motive : List ℕ → Sort u_1) (x : List ℕ) (h_1 : Unit → motive []) (h_2 : (h : ℕ) → (t : List ℕ) → motive (h :: t))
(h : ℕ) (t : List ℕ),
x = h :: t →
(match x with
| [] => h_1 ()
| h :: t => h_2 h t) ≍
h_2 h t
theorem Nat.digitsAux0.match_1.congr_eq_1 E1 #
∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : (n : ℕ) → motive n.succ),
x = 0 →
(match x with
| 0 => h_1 ()
| n.succ => h_2 n) ≍
h_1 ()
def Nat.digitsAux0 E1 #
ℕ → List ℕ
(Impl.) An auxiliary definition for digits, to help get the desired definitional unfolding.
def Nat.digitsAux1 E1 #
ℕ → List ℕ
(Impl.) An auxiliary definition for digits, to help get the desired definitional unfolding.
def Nat.digitsAux E0 #
(b : ℕ) → 2 ≤ b → ℕ → List ℕ
(Impl.) An auxiliary definition for digits, to help get the desired definitional unfolding.
theorem Nat.digitsAux_zero E0 #
∀ (b : ℕ) (h : 2 ≤ b), b.digitsAux h 0 = []
theorem Nat.digitsAux_def E0 #
∀ (b : ℕ) (h : 2 ≤ b) (n : ℕ), 0 < n → b.digitsAux h n = n % b :: b.digitsAux h (n / b)
def Nat.digits E1 #
ℕ → ℕ → List ℕ
digits b n gives the digits, in little-endian order,
of a natural number n in a specified base b.
In any base, we have ofDigits b L = L.foldr (fun x y ↦ x + b * y) 0.
- For any
2 ≤ b, we havel < bfor anyl ∈ digits b n, and the last digit is not zero. This uniquely specifies the behaviour ofdigits b. - For
b = 1, we definedigits 1 n = List.replicate n 1. - For
b = 0, we definedigits 0 n = [n], exceptdigits 0 0 = [].
Note this differs from the existing Nat.toDigits in core, which is used for printing numerals.
In particular, Nat.toDigits b 0 = ['0'], while digits b 0 = [].
theorem Nat.digits_zero E0 #
∀ (b : ℕ), b.digits 0 = []
theorem Nat.digits_zero_zero E1 #
Nat.digits 0 0 = []
theorem Nat.digits_zero_succ E0 #
∀ (n : ℕ), Nat.digits 0 n.succ = [n + 1]
theorem Nat.digits_zero_succ' E1 #
∀ {n : ℕ}, n ≠ 0 → Nat.digits 0 n = [n]
theorem Nat.digits_one E0 #
∀ (n : ℕ), Nat.digits 1 n = List.replicate n 1
theorem Nat.digits_one_succ E1 #
∀ (n : ℕ), Nat.digits 1 (n + 1) = 1 :: Nat.digits 1 n
theorem Nat.digits_add_two_add_one E1 #
∀ (b n : ℕ), (b + 2).digits (n + 1) = (n + 1) % (b + 2) :: (b + 2).digits ((n + 1) / (b + 2))
theorem Nat.digits_of_two_le_of_pos E0 #
∀ {n b : ℕ}, 2 ≤ b → 0 < n → b.digits n = n % b :: b.digits (n / b)
theorem Nat.digits_def' E1 #
∀ {b : ℕ}, 1 < b → ∀ {n : ℕ}, 0 < n → b.digits n = n % b :: b.digits (n / b)
theorem Nat.digits_of_lt E0 #
∀ (b x : ℕ), x ≠ 0 → x < b → b.digits x = [x]
theorem Nat.digits_add E1 #
∀ (b : ℕ), 1 < b → ∀ (x y : ℕ), x < b → x ≠ 0 ∨ y ≠ 0 → b.digits (x + b * y) = x :: b.digits y
def Nat.ofDigits E1 #
{α : Type u_1} → [Semiring α] → α → List ℕ → α
ofDigits b L takes a list L of natural numbers, and interprets them
as a number in semiring, as the little-endian digits in base b.
theorem Nat.ofDigits_eq_foldr E1 #
∀ {α : Type u_1} [inst : Semiring α] (b : α) (L : List ℕ), Nat.ofDigits b L = List.foldr (fun x y ↦ ↑x + b * y) 0 L
theorem Nat.ofDigits_nil E0 #
∀ {b : ℕ}, Nat.ofDigits b [] = 0
theorem Nat.ofDigits_singleton E0 #
∀ {b n : ℕ}, Nat.ofDigits b [n] = n
theorem Nat.ofDigits_one_cons E0 #
∀ {α : Type u_1} [inst : Semiring α] (h : ℕ) (L : List ℕ), Nat.ofDigits 1 (h :: L) = ↑h + Nat.ofDigits 1 L
theorem Nat.ofDigits_cons E1 #
∀ {b hd : ℕ} {tl : List ℕ}, Nat.ofDigits b (hd :: tl) = hd + b * Nat.ofDigits b tl
theorem Nat.ofDigits_append E1 #
∀ {b : ℕ} {l1 l2 : List ℕ}, Nat.ofDigits b (l1 ++ l2) = Nat.ofDigits b l1 + b ^ l1.length * Nat.ofDigits b l2
theorem Nat.ofDigits_append_zero E0 #
∀ {b : ℕ} (l : List ℕ), Nat.ofDigits b (l ++ [0]) = Nat.ofDigits b l
theorem Nat.ofDigits_replicate_zero E0 #
∀ {b k : ℕ}, Nat.ofDigits b (List.replicate k 0) = 0
theorem Nat.ofDigits_append_replicate_zero E0 #
∀ {b k : ℕ} (l : List ℕ), Nat.ofDigits b (l ++ List.replicate k 0) = Nat.ofDigits b l
theorem Nat.ofDigits_reverse_cons E1 #
∀ {b : ℕ} (l : List ℕ) (d : ℕ), Nat.ofDigits b (d :: l).reverse = Nat.ofDigits b l.reverse + b ^ l.length * d
theorem Nat.ofDigits_reverse_zero_cons E1 #
∀ {b : ℕ} (l : List ℕ), Nat.ofDigits b (0 :: l).reverse = Nat.ofDigits b l.reverse
theorem Nat.coe_ofDigits E0 #
∀ (α : Type u_1) [inst : Semiring α] (b : ℕ) (L : List ℕ), ↑(Nat.ofDigits b L) = Nat.ofDigits (↑b) L
theorem Nat.digits_zero_of_eq_zero E1 #
∀ {b : ℕ}, b ≠ 0 → ∀ {L : List ℕ}, Nat.ofDigits b L = 0 → ∀ l ∈ L, l = 0
theorem Nat.digits_ofDigits E1 #
∀ (b : ℕ),
1 < b → ∀ (L : List ℕ), (∀ l ∈ L, l < b) → (∀ (h : L ≠ []), L.getLast h ≠ 0) → b.digits (Nat.ofDigits b L) = L
theorem Nat.ofDigits_digits E1 #
∀ (b n : ℕ), Nat.ofDigits b (b.digits n) = n
theorem Nat.ofDigits_one E1 #
∀ (L : List ℕ), Nat.ofDigits 1 L = L.sum
theorem Nat.digits_eq_nil_iff_eq_zero E1 #
∀ {b n : ℕ}, b.digits n = [] ↔ n = 0
theorem Nat.digits_ne_nil_iff_ne_zero E1 #
∀ {b n : ℕ}, b.digits n ≠ [] ↔ n ≠ 0
theorem Nat.digits_eq_cons_digits_div E1 #
∀ {b n : ℕ}, 1 < b → n ≠ 0 → b.digits n = n % b :: b.digits (n / b)
theorem Nat.digits_getLast E1 #
∀ {b : ℕ} (m : ℕ),
1 < b → ∀ (p : b.digits m ≠ []) (q : b.digits (m / b) ≠ []), (b.digits m).getLast p = (b.digits (m / b)).getLast q
theorem Nat.digits.injective E1 #
∀ (b : ℕ), Function.Injective b.digits
theorem Nat.digits_inj_iff E0 #
∀ {b n m : ℕ}, b.digits n = b.digits m ↔ n = m
theorem Nat.mul_ofDigits E1 #
∀ (n : ℕ) {b : ℕ} {l : List ℕ}, n * Nat.ofDigits b l = Nat.ofDigits b (List.map (fun x ↦ n * x) l)
theorem Nat.ofDigits_inj_of_len_eq E0 #
∀ {b : ℕ},
1 < b →
∀ {L1 L2 : List ℕ},
L1.length = L2.length → (∀ l ∈ L1, l < b) → (∀ l ∈ L2, l < b) → Nat.ofDigits b L1 = Nat.ofDigits b L2 → L1 = L2
theorem Nat.ofDigits_add_ofDigits_eq_ofDigits_zipWith_of_length_eq E0 #
∀ {b : ℕ} {l1 l2 : List ℕ},
l1.length = l2.length →
Nat.ofDigits b l1 + Nat.ofDigits b l2 = Nat.ofDigits b (List.zipWith (fun x1 x2 ↦ x1 + x2) l1 l2)
The addition of ofDigits of two lists is equal to ofDigits of digit-wise addition of them
theorem Nat.digits_lt_base' E1 #
∀ {b m d : ℕ}, d ∈ (b + 2).digits m → d < b + 2
The digits in the base b+2 expansion of n are all less than b+2
theorem Nat.digits_lt_base E1 #
∀ {b m d : ℕ}, 1 < b → d ∈ b.digits m → d < b
The digits in the base b expansion of n are all less than b, if b ≥ 2
theorem Nat.ofDigits_lt_base_pow_length' E1 #
∀ {b : ℕ} {l : List ℕ}, (∀ x ∈ l, x < b + 2) → Nat.ofDigits (b + 2) l < (b + 2) ^ l.length
an n-digit number in base b + 2 is less than (b + 2)^n
theorem Nat.ofDigits_lt_base_pow_length E1 #
∀ {b : ℕ} {l : List ℕ}, 1 < b → (∀ x ∈ l, x < b) → Nat.ofDigits b l < b ^ l.length
an n-digit number in base b is less than b^n if b > 1
theorem Nat.lt_base_pow_length_digits' E1 #
∀ {b m : ℕ}, m < (b + 2) ^ ((b + 2).digits m).length
Any number m is less than (b+2)^(number of digits in the base b + 2 representation of m)
theorem Nat.lt_base_pow_length_digits E1 #
∀ {b m : ℕ}, 1 < b → m < b ^ (b.digits m).length
Any number m is less than b^(number of digits in the base b representation of m)
theorem Nat.digits_base_mul E1 #
∀ {b m : ℕ}, 1 < b → 0 < m → b.digits (b * m) = 0 :: b.digits m
theorem Nat.digits_base_pow_mul E1 #
∀ {b k m : ℕ}, 1 < b → 0 < m → b.digits (b ^ k * m) = List.replicate k 0 ++ b.digits m
theorem Nat.ofDigits_digits_append_digits E1 #
∀ {b m n : ℕ}, Nat.ofDigits b (b.digits n ++ b.digits m) = n + b ^ (b.digits n).length * m
theorem Nat.ofDigits_monotone E1 #
∀ {p q : ℕ} (L : List ℕ), p ≤ q → Nat.ofDigits p L ≤ Nat.ofDigits q L
theorem Nat.sum_le_ofDigits E1 #
∀ {p : ℕ} (L : List ℕ), 1 ≤ p → L.sum ≤ Nat.ofDigits p L
theorem Nat.digit_sum_le E1 #
∀ (p n : ℕ), (p.digits n).sum ≤ n
theorem Nat.ofDigits_div_eq_ofDigits_tail E1 #
∀ {p : ℕ}, 0 < p → ∀ (digits : List ℕ), (∀ l ∈ digits, l < p) → Nat.ofDigits p digits / p = Nat.ofDigits p digits.tail
Interpreting as a base p number and dividing by p is the same as interpreting the tail.
theorem Nat.ofDigits_div_pow_eq_ofDigits_drop E1 #
∀ {p : ℕ} (i : ℕ),
0 < p →
∀ (digits : List ℕ), (∀ l ∈ digits, l < p) → Nat.ofDigits p digits / p ^ i = Nat.ofDigits p (List.drop i digits)
Interpreting as a base p number and dividing by p^i is the same as dropping i.
theorem Nat.self_div_pow_eq_ofDigits_drop E1 #
∀ {p : ℕ} (i n : ℕ), 2 ≤ p → n / p ^ i = Nat.ofDigits p (List.drop i (p.digits n))
Dividing n by p^i is like truncating the first i digits of n in base p.
theorem Nat.ofDigits_mod_pow_eq_ofDigits_take E1 #
∀ {p : ℕ} (i : ℕ),
0 < p →
∀ (digits : List ℕ), (∀ l ∈ digits, l < p) → Nat.ofDigits p digits % p ^ i = Nat.ofDigits p (List.take i digits)
Interpreting as a base p number and modulo p^i is the same as taking the first i digits.
theorem Nat.self_mod_pow_eq_ofDigits_take E1 #
∀ {p : ℕ} (i n : ℕ), 2 ≤ p → n % p ^ i = Nat.ofDigits p (List.take i (p.digits n))
n modulo p^i is like taking the least significant i digits of n in base p.
theorem Nat.toDigitsCore_lens_eq_aux E0 #
∀ (b f n : ℕ) (l1 l2 : List Char),
l1.length = l2.length → (b.toDigitsCore f n l1).length = (b.toDigitsCore f n l2).length
theorem Nat.toDigitsCore_lens_eq E0 #
∀ (b f n : ℕ) (c : Char) (tl : List Char), (b.toDigitsCore f n (c :: tl)).length = (b.toDigitsCore f n tl).length + 1
theorem Nat.nat_repr_len_aux E0 #
∀ (n b e : ℕ), 0 < b → n < b ^ e.succ → n / b < b ^ e
theorem Nat.toDigitsCore_length E1 #
∀ (b f n e : ℕ), 0 < e → n < b ^ e → (b.toDigitsCore f n []).length ≤ e
The String representation produced by toDigitsCore has the proper length relative to
the number of digits in n < e for some base b. Since this works with any base,
it can be used for binary, decimal, and hex.
theorem Nat.toDigits_length E1 #
∀ (b n e : ℕ), 0 < e → n < b ^ e → (b.toDigits n).length ≤ e
The core implementation of Nat.toDigits returns a String with length less than or equal to the
number of digits in the base-b number (represented by e). For example, the string
representation of any number less than b ^ 3 has a length less than or equal to 3.
theorem Nat.repr_length E1 #
∀ (n e : ℕ), 0 < e → n < 10 ^ e → n.repr.length ≤ e
The core implementation of Nat.repr returns a String with length less than or equal to the
number of digits in the decimal number (represented by e). For example, the decimal string
representation of any number less than 1000 (10 ^ 3) has a length less than or equal to 3.