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.
(Impl.) An auxiliary definition for digits, to help get the desired definitional unfolding.
Equations
- n.digitsAux1 = List.replicate n 1
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 < 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 = [].
Equations
- Nat.digits 0 = Nat.digitsAux0
- Nat.digits 1 = Nat.digitsAux1
- b.succ.succ.digits = (b + 2).digitsAux ⋯
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
Nat.toDigits length #
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.