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 / perfect-totient-numbers.wiki

82 lines · 1667 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 first "first 20 perfect totient numbers:"
9note ph n is Euler's totient for n below 6000 (the 20th perfect totient number is 5571)
10global ph ulong 6000
11note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12fn emit a view uchar * to void
13o = ulong 0
14while lt o len a bound 4096
15k = write 1 a o sub len a o
16if le k 0
17exit 1
18end
19o = add o ulong k
20end
21end
22note emitu writes n in decimal; 20 digits hold every ulong
23fn emitu n ulong to void
24d = new uchar 20 0
25i = ulong 20
26v = n
27while true bound 20
28i = sub i 1
29put d i uchar add rem v 10 48
30v = div v 10
31if eq v 0
32break
33end
34end
35emit part d i sub 20 i
36end
37fn sieve to void
38for i ulong 6000
39put ph i i
40end
41p = ulong 2
42while lt p 6000 bound 6000
43if eq get ph p p
44j = p
45while lt j 6000 bound 6000
46put ph j sub get ph j div get ph j p
47j = add j p
48end
49end
50p = add p 1
51end
52end
53note n is perfect totient when phi(n) + phi(phi(n)) + ... down to 1 adds up to n; each step at least halves
54note an even value, so 64 steps are plenty
55fn main to int
56sieve
57emit first
58c = ulong 0
59n = ulong 2
60while lt c 20 bound 5999
61s = ulong 0
62v = get ph n
63while gt v 1 bound 64
64s = add s v
65v = get ph v
66end
67s = add s 1
68if eq s n
69emit sp
70emitu n
71c = add c 1
72end
73n = add n 1
74end
75emit nl
76ret 0
77end
78</syntaxhighlight>
79{{out}}
80<pre>
81first 20 perfect totient numbers: 3 9 15 27 39 81 111 183 243 255 327 363 471 729 2187 2199 3063 4359 4375 5571
82</pre>