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/tasks/totient-function/totient-function.om # 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 / tasks / totient-function / totient-function.om` 87 lines ยท 1578 bytes Raw Blame History 1 note Rosetta Code task "Totient function": https://rosettacode.org/wiki/Totient_function 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text phi "phi(" 6 text eqs ") = " 7 text prm ", prime" 8 text cnt "primes up to " 9 text col ": " 10 note ph n is Euler's totient of n for n up to 100000 11 global ph ulong 100001 12 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 13 fn emit a view uchar * to void 14 o = ulong 0 15 while lt o len a bound 4096 16 k = write 1 a o sub len a o 17 if le k 0 18 exit 1 19 end 20 o = add o ulong k 21 end 22 end 23 note emitu writes n in decimal; 20 digits hold every ulong 24 fn emitu n ulong to void 25 d = new uchar 20 0 26 i = ulong 20 27 v = n 28 while true bound 20 29 i = sub i 1 30 put d i uchar add rem v 10 48 31 v = div v 10 32 if eq v 0 33 break 34 end 35 end 36 emit part d i sub 20 i 37 end 38 note sieve: start with ph n = n; each prime p (still ph p = p when reached) scales its multiples by (1 - 1/p) 39 note bound: each p touches at most 100000 / p multiples 40 fn sieve to void 41 for i ulong 100001 42 put ph i i 43 end 44 p = ulong 2 45 while le p 100000 bound 100000 46 if eq get ph p p 47 j = p 48 while le j 100000 bound 100000 49 put ph j sub get ph j div get ph j p 50 j = add j p 51 end 52 end 53 p = add p 1 54 end 55 end 56 note n > 1 is prime exactly when phi(n) = n - 1 57 fn main to int 58 sieve 59 for i ulong 25 60 n = add i 1 61 emit phi 62 emitu n 63 emit eqs 64 emitu get ph n 65 if eq get ph n sub n 1 66 emit prm 67 end 68 emit nl 69 end 70 lim = ulong 100 71 c = ulong 0 72 for i ulong 99999 73 n = add i 2 74 if eq get ph n sub n 1 75 c = add c 1 76 end 77 if eq n lim 78 emit cnt 79 emitu lim 80 emit col 81 emitu c 82 emit nl 83 lim = mul lim 10 84 end 85 end 86 ret 0 87 end