Digital root: Difference between revisions

add OCaml
(Added EasyLang implementation)
(add OCaml)
Line 2,874:
588225 has additive persistence 2 and digital root of 3
393900588225 has additive persistence 2 and digital root of 9</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec digit_sum b n =
if n < b then n else digit_sum b (n / b) + n mod b
 
let digital_root b n =
let rec loop a x =
if x < b then a, x else loop (succ a) (digit_sum b x)
in
loop 0 n
 
let () =
let pr_fmt n (p, r) =
Printf.printf "%u: additive persistence = %u, digital root = %u\n" n p r
in
List.iter
(fun n -> pr_fmt n (digital_root 10 n))
[627615; 39390; 588225; 393900588225]</syntaxhighlight>
{{out}}
<pre>
627615: additive persistence = 2, digital root = 9
39390: additive persistence = 2, digital root = 6
588225: additive persistence = 2, digital root = 3
393900588225: additive persistence = 2, digital root = 9
</pre>
 
=={{header|Oforth}}==
559

edits