Documentation

Mathlib.Data.Nat.Digits.Lemmas

Digits of a natural number #

This provides lemma about the digits of natural numbers.

theorem Nat.ofDigits_eq_sum_mapIdx_aux (b : ) (l : List ) :
(List.zipWith (fun (a i : ) => a * b ^ (i + 1)) l (List.range l.length)).sum = b * (List.zipWith (fun (a i : ) => a * b ^ i) l (List.range l.length)).sum
theorem Nat.ofDigits_eq_sum_mapIdx (b : ) (L : List ) :
ofDigits b L = (List.mapIdx (fun (i a : ) => a * b ^ i) L).sum

Properties #

This section contains various lemmas of properties relating to digits and ofDigits.

theorem Nat.digits_len (b n : ) (hb : 1 < b) (hn : n 0) :
(b.digits n).length = log b n + 1
theorem Nat.digits_length_le_iff {b k : } (hb : 1 < b) (n : ) :
(b.digits n).length k n < b ^ k
theorem Nat.lt_digits_length_iff {b k : } (hb : 1 < b) (n : ) :
k < (b.digits n).length b ^ k n
theorem Nat.getLast_digit_ne_zero (b : ) {m : } (hm : m 0) :
(b.digits m).getLast 0
theorem Nat.digits_append_digits {b m n : } (hb : 0 < b) :
b.digits n ++ b.digits m = b.digits (n + b ^ (b.digits n).length * m)
theorem Nat.digits_append_zeroes_append_digits {b k m n : } (hb : 1 < b) (hm : 0 < m) :
b.digits n ++ List.replicate k 0 ++ b.digits m = b.digits (n + b ^ ((b.digits n).length + k) * m)
theorem Nat.le_digits_len_le (b n m : ) (h : n m) :
theorem Nat.pow_length_le_mul_ofDigits {b : } {l : List } (hl : l []) (hl2 : l.getLast hl 0) :
(b + 2) ^ l.length (b + 2) * ofDigits (b + 2) l
theorem Nat.base_pow_length_digits_le' (b m : ) (hm : m 0) :
(b + 2) ^ ((b + 2).digits m).length (b + 2) * m

Any non-zero natural number m is greater than (b+2)^((number of digits in the base (b+2) representation of m) - 1)

theorem Nat.base_pow_length_digits_le (b m : ) (hb : 1 < b) :
m 0b ^ (b.digits m).length b * m

Any non-zero natural number m is greater than b^((number of digits in the base b representation of m) - 1)

theorem Nat.sub_one_mul_sum_div_pow_eq_sub_sum_digits {p : } (L : List ) {h_nonempty : L []} (h_ne_zero : L.getLast h_nonempty 0) (h_lt : lL, l < p) :
(p - 1) * iFinset.range L.length, ofDigits p L / p ^ i.succ = ofDigits p L - L.sum
theorem Nat.sub_one_mul_sum_log_div_pow_eq_sub_sum_digits {p : } (n : ) :
(p - 1) * iFinset.range (log p n).succ, n / p ^ i.succ = n - (p.digits n).sum

Binary #

theorem Nat.digits_two_eq_bits (n : ) :
digits 2 n = List.map (fun (b : Bool) => bif b then 1 else 0) n.bits

Modular Arithmetic #

theorem Nat.dvd_ofDigits_sub_ofDigits {α : Type u_1} [CommRing α] {a b k : α} (h : k a - b) (L : List ) :
theorem Nat.ofDigits_modEq' (b b' k : ) (h : b b' [MOD k]) (L : List ) :
theorem Nat.ofDigits_modEq (b k : ) (L : List ) :
ofDigits b L ofDigits (b % k) L [MOD k]
theorem Nat.ofDigits_mod (b k : ) (L : List ) :
ofDigits b L % k = ofDigits (b % k) L % k
theorem Nat.ofDigits_mod_eq_head! (b : ) (l : List ) :
ofDigits b l % b = l.head! % b
theorem Nat.head!_digits {b n : } (h : b 1) :
(b.digits n).head! = n % b
theorem Nat.ofDigits_zmodeq' (b b' : ) (k : ) (h : b b' [ZMOD k]) (L : List ) :
ofDigits b L ofDigits b' L [ZMOD k]
theorem Nat.ofDigits_zmodeq (b : ) (k : ) (L : List ) :
ofDigits b L ofDigits (b % k) L [ZMOD k]
theorem Nat.ofDigits_zmod (b : ) (k : ) (L : List ) :
ofDigits b L % k = ofDigits (b % k) L % k
theorem Nat.modEq_digits_sum (b b' : ) (h : b' % b = 1) (n : ) :
n (b'.digits n).sum [MOD b]
theorem Nat.zmodeq_ofDigits_digits (b b' : ) (c : ) (h : b' c [ZMOD b]) (n : ) :
n ofDigits c (b'.digits n) [ZMOD b]
theorem Nat.ofDigits_neg_one (L : List ) :
ofDigits (-1) L = (List.map (fun (n : ) => n) L).alternatingSum
theorem Nat.getD_digits (n i : ) {b : } (h : 2 b) :
(b.digits n).getD i 0 = n / b ^ i % b

Explicit computation of the i-th digit of n in base b.

Bijection #

def Nat.digitsAppend (b l n : ) :

The list of digits of n in base b with some 0's appended so that its length is equal to l if it is < l. This is an inverse function of Nat.ofDigits for n < b ^ l, see Nat.setInvOn_digitsAppend_ofDigits. If n ≥ b ^ l, then the list of digits of n in base b is of length at least l and this function just return b.digits n.

Equations
Instances For
    theorem Nat.length_digitsAppend {n b : } (hb : 1 < b) (l : ) (hn : n < b ^ l) :
    theorem Nat.lt_of_mem_digitsAppend {n b : } (hb : 1 < b) (l i : ) (hi : i b.digitsAppend l n) :
    i < b
    theorem Nat.mapsTo_ofDigits {b : } (hb : 1 < b) (l : ) :
    Set.MapsTo (ofDigits b) {L : List | L.length = l xL, x < b} {n : | n < b ^ l}
    theorem Nat.mapsTo_digitsAppend {b : } (hb : 1 < b) (l : ) :
    Set.MapsTo (b.digitsAppend l) {n : | n < b ^ l} {L : List | L.length = l xL, x < b}
    theorem Nat.injOn_ofDigits {b : } (hb : 1 < b) (l : ) :
    Set.InjOn (ofDigits b) {L : List | L.length = l xL, x < b}
    theorem Nat.setInvOn_digitsAppend_ofDigits {b : } (hb : 1 < b) (l : ) :
    Set.InvOn (b.digitsAppend l) (ofDigits b) {L : List | L.length = l xL, x < b} {n : | n < b ^ l}
    theorem Nat.bijOn_ofDigits {b : } (hb : 1 < b) (l : ) :
    Set.BijOn (ofDigits b) {L : List | L.length = l xL, x < b} {n : | n < b ^ l}

    The map L ↦ Nat.ofDigits b L is bijection between the set of lists of natural integers of length l with coefficients < b to the set of natural integers < b ^ l.

    theorem Nat.bijOn_digitsAppend {b : } (hb : 1 < b) (l : ) :
    Set.BijOn (b.digitsAppend l) {n : | n < b ^ l} {L : List | L.length = l xL, x < b}

    The map n ↦ Nat.digitsAppend b L is bijection between the set of natural integers < b ^ l to the set of lists of natural integers of length l with coefficients < b to .

    theorem Nat.sum_digits_ofDigits_eq_sum {b : } (hb : 1 < b) {l : } {L : List } (hL : L {L : List | L.length = l xL, x < b}) :
    (b.digits (ofDigits b L)).sum = L.sum
    noncomputable def List.fixedLengthDigits {b : } (hb : 1 < b) (l : ) :

    The set of lists of natural integers of length l with coefficients < b as a Finset. This can be seen as the set of lists of length l of the digits in base b of the integers < b ^ l. Having this set as a Finset can be helpful for some proofs.

    Equations
    Instances For
      theorem List.mem_fixedLengthDigits_iff {b : } (hb : 1 < b) {l : } {L : List } :
      L fixedLengthDigits hb l L.length = l xL, x < b
      theorem Nat.bijOn_ofDigits' {b : } (hb : 1 < b) (l : ) :

      The bijection Nat.bijOn_ofDigits stated as a bijection between Finset. This spelling can be helpful for some proofs.

      theorem Nat.bijOn_digitsAppend' {b : } (hb : 1 < b) (l : ) :

      The bijection Nat.bijOn_digitsAppend stated as a bijection between Finset. This spelling can be helpful for some proofs.

      @[simp]
      theorem List.fixedLengthDigits_zero {b : } (hb : 1 < b) :
      @[simp]
      theorem List.fixedLengthDigits_one {b : } (hb : 1 < b) :
      theorem List.card_fixedLengthDigits {b : } (hb : 1 < b) (l : ) :
      noncomputable def List.consFixedLengthDigits {b : } (hb : 1 < b) (l d : ) :

      The Finset of lists whose head is a fixed integer d and tail is a list in List.fixedLengthDigits b l.

      Equations
      Instances For
        theorem List.ne_empty_of_mem_consFixedLengthDigits {b : } (hb : 1 < b) {l d : } {L : List } (hL : L consFixedLengthDigits hb l d) :
        theorem List.consFixedLengthDigits_head {b : } (hb : 1 < b) {l d : } {L : List } (hL : L consFixedLengthDigits hb l d) :
        L.head = d
        theorem List.cons_mem_fixedLengthDigits_succ {b : } (hb : 1 < b) (l d : ) (hd : d < b) {L : List } (hL : L fixedLengthDigits hb l) :
        d :: L fixedLengthDigits hb (l + 1)

        If L is a list in List.fixedLengthDigits b l and d is an integer < b, then d :: L is a list in List.fixedLengthDigits b (l + 1).

        The set List.fixedLengthDigits b (l + 1) is the disjoint union of the sets List.consFixedLengthDigits b l d where d ranges through the natural integers < d.

        theorem List.sum_fixedLengthDigits_sum {b : } (hb : 1 < b) (l : ) :
        LfixedLengthDigits hb l, L.sum = l * b ^ (l - 1) * b.choose 2
        theorem Nat.sum_sum_digits_eq {b : } (hb : 1 < b) (l : ) :
        xFinset.range (b ^ l), (b.digits x).sum = l * b ^ (l - 1) * b.choose 2

        The formula for the sum of the sum of the digits in base b over the natural integers < b ^ l.