Digits of a natural number #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
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/.
A basic norm_digits
tactic is also provided for proving goals of the form
nat.digits a b = l
where a
and b
are numerals.
(Impl.) An auxiliary definition for digits
, to help get the desired definitional unfolding.
Equations
- (n + 1).digits_aux_0 = [n + 1]
- 0.digits_aux_0 = list.nil
(Impl.) An auxiliary definition for digits
, to help get the desired definitional unfolding.
Equations
- n.digits_aux_1 = list.replicate n 1
(Impl.) An auxiliary definition for digits
, to help get the desired definitional unfolding.
Equations
- b.digits_aux h (n + 1) = (n + 1) % b :: b.digits_aux h ((n + 1) / b)
- b.digits_aux h 0 = list.nil
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 of_digits b L = L.foldr (λ x y, x + b * y) 0
.
- For any
2 ≤ b
, we havel < b
for 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.to_digits
in core, which is used for printing numerals.
In particular, nat.to_digits b 0 = [0]
, while digits b 0 = []
.
Equations
- (b + 2).digits = (b + 2).digits_aux _
- 1.digits = nat.digits_aux_1
- 0.digits = nat.digits_aux_0
of_digits 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
.
Equations
- nat.of_digits b (h :: t) = ↑h + b * nat.of_digits b t
- nat.of_digits b list.nil = 0
Properties #
This section contains various lemmas of properties relating to digits
and of_digits
.