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/.
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
- Nat.digitsAux0 x = match x with | 0 => [] | Nat.succ n => [n + 1]
Instances For
(Impl.) An auxiliary definition for digits
, to help get the desired definitional unfolding.
Equations
- Nat.digitsAux1 n = List.replicate n 1
Instances For
(Impl.) An auxiliary definition for digits
, to help get the desired definitional unfolding.
Equations
- Nat.digitsAux b h 0 = []
- Nat.digitsAux b h (Nat.succ n) = (n + 1) % b :: Nat.digitsAux b h ((n + 1) / b)
Instances For
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 < 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
- Nat.digits x = match (motive := ℕ → ℕ → List ℕ) x with | 0 => Nat.digitsAux0 | 1 => Nat.digitsAux1 | Nat.succ (Nat.succ b) => Nat.digitsAux (b + 2) (_ : 2 ≤ b + 2)
Instances For
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
.
Equations
- Nat.ofDigits b [] = 0
- Nat.ofDigits b (h :: t) = ↑h + b * Nat.ofDigits b t
Instances For
The digits in the base b+2 expansion of n are all less than b+2
The digits in the base b expansion of n are all less than b, if b ≥ 2
an n-digit number in base b + 2 is less than (b + 2)^n
an n-digit number in base b is less than b^n if b > 1
Any number m is less than (b+2)^(number of digits in the base b + 2 representation of m)
Any number m is less than b^(number of digits in the base b representation of m)
Any non-zero natural number m
is greater than
(b+2)^((number of digits in the base (b+2) representation of m) - 1)
Any non-zero natural number m
is greater than
b^((number of digits in the base b representation of m) - 1)
Interpreting as a base p
number and dividing by p
is the same as interpreting the tail.
Interpreting as a base p
number and dividing by p^i
is the same as dropping i
.
Dividing n
by p^i
is like truncating the first i
digits of n
in base p
.
Binary #
Modular Arithmetic #
Divisibility #
Divisibility by 3 Rule