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
- x.digits = match (motive := ℕ → ℕ → List ℕ) x with | 0 => Nat.digitsAux0 | 1 => Nat.digitsAux1 | b.succ.succ => (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
The addition of ofDigits of two lists is equal to ofDigits of digit-wise addition of them
an n-digit number in base b is less than b^n if b > 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.
Binary #
Modular Arithmetic #
Divisibility #
Nat.toDigits length #
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 greater
than one, it can be used for binary, decimal, and hex.
Alias of Nat.toDigitsCore_length.
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 greater
than one, it can be used for binary, decimal, and hex.
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.