38 lines
805 B
Text
38 lines
805 B
Text
include "cowgol.coh";
|
|
|
|
# Calculate the digital root and additive persistance of a number
|
|
# in a given base
|
|
sub digital_root(n: uint32, base: uint32): (root: uint32, pers: uint8) is
|
|
pers := 0;
|
|
while base < n loop
|
|
var step: uint32 := 0;
|
|
while n > 0 loop
|
|
step := step + (n % base);
|
|
n := n / base;
|
|
end loop;
|
|
pers := pers + 1;
|
|
n := step;
|
|
end loop;
|
|
root := n;
|
|
end sub;
|
|
|
|
# Print digital root and persistence (in base 10)
|
|
sub test(n: uint32) is
|
|
var root: uint32;
|
|
var pers: uint8;
|
|
|
|
(root, pers) := digital_root(n, 10);
|
|
|
|
print_i32(n);
|
|
print(": root = ");
|
|
print_i32(root);
|
|
print(", persistence = ");
|
|
print_i8(pers);
|
|
print_nl();
|
|
end sub;
|
|
|
|
test(4);
|
|
test(627615);
|
|
test(39390);
|
|
test(588225);
|
|
test(9992);
|