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

117 lines · 2078 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 phi "phi("
8text eqs ") = "
9text prm ", prime"
10text cnt "primes up to "
11text col ": "
12note ph n is Euler's totient of n for n up to 100000
13global ph ulong 100001
14note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15fn emit a view uchar * to void
16o = ulong 0
17while lt o len a bound 4096
18k = write 1 a o sub len a o
19if le k 0
20exit 1
21end
22o = add o ulong k
23end
24end
25note emitu writes n in decimal; 20 digits hold every ulong
26fn emitu n ulong to void
27d = new uchar 20 0
28i = ulong 20
29v = n
30while true bound 20
31i = sub i 1
32put d i uchar add rem v 10 48
33v = div v 10
34if eq v 0
35break
36end
37end
38emit part d i sub 20 i
39end
40note sieve: start with ph n = n; each prime p (still ph p = p when reached) scales its multiples by (1 - 1/p)
41note bound: each p touches at most 100000 / p multiples
42fn sieve to void
43for i ulong 100001
44put ph i i
45end
46p = ulong 2
47while le p 100000 bound 100000
48if eq get ph p p
49j = p
50while le j 100000 bound 100000
51put ph j sub get ph j div get ph j p
52j = add j p
53end
54end
55p = add p 1
56end
57end
58note n > 1 is prime exactly when phi(n) = n - 1
59fn main to int
60sieve
61for i ulong 25
62n = add i 1
63emit phi
64emitu n
65emit eqs
66emitu get ph n
67if eq get ph n sub n 1
68emit prm
69end
70emit nl
71end
72lim = ulong 100
73c = ulong 0
74for i ulong 99999
75n = add i 2
76if eq get ph n sub n 1
77c = add c 1
78end
79if eq n lim
80emit cnt
81emitu lim
82emit col
83emitu c
84emit nl
85lim = mul lim 10
86end
87end
88ret 0
89end
90</syntaxhighlight>
91{{out}}
92<pre>
93phi(1) = 1
94phi(2) = 1, prime
95phi(3) = 2, prime
96phi(4) = 2
97phi(5) = 4, prime
98phi(6) = 2
99phi(7) = 6, prime
100phi(8) = 4
101phi(9) = 6
102phi(10) = 4
103phi(11) = 10, prime
104phi(12) = 4
105phi(13) = 12, prime
106phi(14) = 6
107phi(15) = 8
108...
109phi(22) = 10
110phi(23) = 22, prime
111phi(24) = 8
112phi(25) = 20
113primes up to 100: 25
114primes up to 1000: 168
115primes up to 10000: 1229
116primes up to 100000: 9592
117</pre>