interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / factors-of-an-integer / factors-of-an-integer.om

72 lines · 1351 bytes Raw Blame History
1note Rosetta Code task "Factors of an integer": https://rosettacode.org/wiki/Factors_of_an_integer
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text pre "factors of "
7text col ":"
8note emit writes every byte of a to stdout; a short write is retried, an error exits 1
9fn emit a view uchar * to void
10o = ulong 0
11while lt o len a bound 4096
12k = write 1 a o sub len a o
13if le k 0
14exit 1
15end
16o = add o ulong k
17end
18end
19note emitu writes n in decimal; 20 digits hold every ulong
20fn emitu n ulong to void
21d = new uchar 20 0
22i = ulong 20
23v = n
24while true bound 20
25i = sub i 1
26put d i uchar add rem v 10 48
27v = div v 10
28if eq v 0
29break
30end
31end
32emit part d i sub 20 i
33end
34note divisors d with d * d <= n come out ascending; their partners n / d, read back in reverse, finish the list
35note bound: the largest n here is 600851475143, so d stays below 775147, and no n has more than 64 small divisors
36fn tell n ulong to void
37lo = new ulong 64 0
38k = ulong 0
39d = ulong 1
40while le mul d d n bound 999999
41if eq rem n d 0
42put lo k d
43k = add k 1
44end
45d = add d 1
46end
47emit pre
48emitu n
49emit col
50for i ulong k
51emit sp
52emitu get lo i
53end
54for i ulong k
55e = get lo sub sub k 1 i
56if ne mul e e n
57emit sp
58emitu div n e
59end
60end
61emit nl
62end
63fn main to int
64tell 1
65tell 45
66tell 53
67tell 64
68tell 720
69tell 1000000
70tell 600851475143
71ret 0
72end