interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / digital-root / digital-root.om

68 lines · 1230 bytes Raw Blame History
1note Rosetta Code task "Digital root": https://rosettacode.org/wiki/Digital_root
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text per ": additive persistence "
6text dr ", digital root "
7note emit writes every byte of a to stdout; a short write is retried, an error exits 1
8fn emit a view uchar * to void
9o = ulong 0
10while lt o len a bound 4096
11k = write 1 a o sub len a o
12if le k 0
13exit 1
14end
15o = add o ulong k
16end
17end
18note emitu writes n in decimal; 20 digits hold every ulong
19fn emitu n ulong to void
20d = new uchar 20 0
21i = ulong 20
22v = n
23while true bound 20
24i = sub i 1
25put d i uchar add rem v 10 48
26v = div v 10
27if eq v 0
28break
29end
30end
31emit part d i sub 20 i
32end
33note a ulong has at most 20 digits
34fn dsum n ulong to ulong
35s = ulong 0
36v = n
37while gt v 0 bound 20
38s = add s rem v 10
39v = div v 10
40end
41ret s
42end
43note bound: one digit sum takes any ulong below 181, the next below 19, then a single digit, so 4 rounds suffice
44fn tell n ulong to void
45v = n
46p = ulong 0
47while gt v 9 bound 4
48v = dsum v
49p = add p 1
50end
51emitu n
52emit per
53emitu p
54emit dr
55emitu v
56emit nl
57end
58fn main to int
59tell 627615
60tell 39390
61tell 588225
62tell 393900588225
63tell 0
64tell 9
65tell 199
66tell 18446744073709551615
67ret 0
68end