interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / abundant-deficient-and-perfect-number-classifications / abundant-deficient-and-perfect-number-classifications.om

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