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