omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / abundant-deficient-and-perfect-number-classifications / abundant-deficient-and-perfect-number-classifications.om
| 1 | note Rosetta Code task "Abundant, deficient and perfect number classifications": https://rosettacode.org/wiki/Abundant,_deficient_and_perfect_number_classifications |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text tdef "deficient: " |
| 6 | text tper "perfect: " |
| 7 | text tabu "abundant: " |
| 8 | note ds n is the sum of the proper divisors of n, for n up to 20000 |
| 9 | global ds ulong 20001 |
| 10 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 11 | fn emit a view uchar * to void |
| 12 | o = ulong 0 |
| 13 | while lt o len a bound 4096 |
| 14 | k = write 1 a o sub len a o |
| 15 | if le k 0 |
| 16 | exit 1 |
| 17 | end |
| 18 | o = add o ulong k |
| 19 | end |
| 20 | end |
| 21 | note emitu writes n in decimal; 20 digits hold every ulong |
| 22 | fn emitu n ulong to void |
| 23 | d = new uchar 20 0 |
| 24 | i = ulong 20 |
| 25 | v = n |
| 26 | while true bound 20 |
| 27 | i = sub i 1 |
| 28 | put d i uchar add rem v 10 48 |
| 29 | v = div v 10 |
| 30 | if eq v 0 |
| 31 | break |
| 32 | end |
| 33 | end |
| 34 | emit part d i sub 20 i |
| 35 | end |
| 36 | note each i adds itself to its multiples 2i, 3i, ... up to 20000; fewer than 20000 multiples per i |
| 37 | fn sieve to void |
| 38 | i = ulong 1 |
| 39 | while le i 10000 bound 10000 |
| 40 | j = mul i 2 |
| 41 | while le j 20000 bound 20000 |
| 42 | put ds j add get ds j i |
| 43 | j = add j i |
| 44 | end |
| 45 | i = add i 1 |
| 46 | end |
| 47 | end |
| 48 | note 1..20000 sorted by comparing each n with the sum of its proper divisors |
| 49 | fn main to int |
| 50 | sieve |
| 51 | d = ulong 0 |
| 52 | p = ulong 0 |
| 53 | a = ulong 0 |
| 54 | for i ulong 20000 |
| 55 | n = add i 1 |
| 56 | s = get ds n |
| 57 | if lt s n |
| 58 | d = add d 1 |
| 59 | end |
| 60 | if eq s n |
| 61 | p = add p 1 |
| 62 | end |
| 63 | if gt s n |
| 64 | a = add a 1 |
| 65 | end |
| 66 | end |
| 67 | emit tdef |
| 68 | emitu d |
| 69 | emit nl |
| 70 | emit tper |
| 71 | emitu p |
| 72 | emit nl |
| 73 | emit tabu |
| 74 | emitu a |
| 75 | emit nl |
| 76 | ret 0 |
| 77 | end |