Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/totient-function.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
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}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text phi "phi("
8 text eqs ") = "
9 text prm ", prime"
10 text cnt "primes up to "
11 text col ": "
12 note ph n is Euler's totient of n for n up to 100000
13 global ph ulong 100001
14 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15 fn emit a view uchar * to void
16 o = ulong 0
17 while lt o len a bound 4096
18 k = write 1 a o sub len a o
19 if le k 0
20 exit 1
21 end
22 o = add o ulong k
23 end
24 end
25 note emitu writes n in decimal; 20 digits hold every ulong
26 fn emitu n ulong to void
27 d = new uchar 20 0
28 i = ulong 20
29 v = n
30 while true bound 20
31 i = sub i 1
32 put d i uchar add rem v 10 48
33 v = div v 10
34 if eq v 0
35 break
36 end
37 end
38 emit part d i sub 20 i
39 end
40 note sieve: start with ph n = n; each prime p (still ph p = p when reached) scales its multiples by (1 - 1/p)
41 note bound: each p touches at most 100000 / p multiples
42 fn sieve to void
43 for i ulong 100001
44 put ph i i
45 end
46 p = ulong 2
47 while le p 100000 bound 100000
48 if eq get ph p p
49 j = p
50 while le j 100000 bound 100000
51 put ph j sub get ph j div get ph j p
52 j = add j p
53 end
54 end
55 p = add p 1
56 end
57 end
58 note n > 1 is prime exactly when phi(n) = n - 1
59 fn main to int
60 sieve
61 for i ulong 25
62 n = add i 1
63 emit phi
64 emitu n
65 emit eqs
66 emitu get ph n
67 if eq get ph n sub n 1
68 emit prm
69 end
70 emit nl
71 end
72 lim = ulong 100
73 c = ulong 0
74 for i ulong 99999
75 n = add i 2
76 if eq get ph n sub n 1
77 c = add c 1
78 end
79 if eq n lim
80 emit cnt
81 emitu lim
82 emit col
83 emitu c
84 emit nl
85 lim = mul lim 10
86 end
87 end
88 ret 0
89 end
90
91 {{out}}
92
93 phi(1) = 1
94 phi(2) = 1, prime
95 phi(3) = 2, prime
96 phi(4) = 2
97 phi(5) = 4, prime
98 phi(6) = 2
99 phi(7) = 6, prime
100 phi(8) = 4
101 phi(9) = 6
102 phi(10) = 4
103 phi(11) = 10, prime
104 phi(12) = 4
105 phi(13) = 12, prime
106 phi(14) = 6
107 phi(15) = 8
108 ...
109 phi(22) = 10
110 phi(23) = 22, prime
111 phi(24) = 8
112 phi(25) = 20
113 primes up to 100: 25
114 primes up to 1000: 168
115 primes up to 10000: 1229
116 primes up to 100000: 9592
117