interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / proper-divisors / proper-divisors.om

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