interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / happy-numbers.wiki

75 lines · 1488 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text hdr "first 8 happy numbers:"
9note emit writes every byte of a to stdout; a short write is retried, an error exits 1
10fn emit a view uchar * to void
11o = ulong 0
12while lt o len a bound 4096
13k = write 1 a o sub len a o
14if le k 0
15exit 1
16end
17o = add o ulong k
18end
19end
20note emitu writes n in decimal; 20 digits hold every ulong
21fn emitu n ulong to void
22d = new uchar 20 0
23i = ulong 20
24v = n
25while true bound 20
26i = sub i 1
27put d i uchar add rem v 10 48
28v = div v 10
29if eq v 0
30break
31end
32end
33emit part d i sub 20 i
34end
35fn step n ulong to ulong
36s = ulong 0
37v = n
38while gt v 0 bound 20
39d = rem v 10
40s = add s mul d d
41v = div v 10
42end
43ret s
44end
45note Floyd's tortoise and hare: the iteration is eventually periodic; n is happy when the cycle is 1 -> 1
46note bound: from n < 1000 the values stay below 1000 (a 3-digit square sum is at most 243), so the meeting comes within 1000 steps
47fn happy n ulong to bool
48s = n
49f = step n
50while ne s f bound 1000
51s = step s
52f = step step f
53end
54ret eq s 1
55end
56fn main to int
57emit hdr
58n = ulong 0
59k = ulong 0
60while lt k 8 bound 1000
61n = add n 1
62if happy n
63emit sp
64emitu n
65k = add k 1
66end
67end
68emit nl
69ret 0
70end
71</syntaxhighlight>
72{{out}}
73<pre>
74first 8 happy numbers: 1 7 10 13 19 23 28 31
75</pre>