Add pow10, log10i and digits10 functions

These are used by a couple of solutions now.
This commit is contained in:
2024-12-11 09:16:08 +00:00
parent b9e3907e4d
commit 8bfe33fece
2 changed files with 26 additions and 0 deletions

View File

@@ -66,3 +66,18 @@ module Grid = struct
(String.sub grid.grid (idx + 1) (length grid - idx - 1));
{ grid with grid = Buffer.contents builder }
end
let log10i i =
let rec impl acc = function 0 -> acc | x -> impl (acc + 1) (x / 10) in
assert (i > 0);
impl ~-1 i
let digits10 = function
| 0 -> 1
| n when n > 0 -> 1 + log10i n
| n (* when n < 0 *) -> 1 + log10i (-n)
let pow10 n =
let rec impl acc = function 0 -> acc | x -> impl (acc * 10) (x - 1) in
assert (n >= 0);
impl 1 n