interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / count-in-factors / count-in-factors.om

76 lines · 1269 bytes Raw Blame History
1note Rosetta Code task "Count in factors": https://rosettacode.org/wiki/Count_in_factors
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text x " x "
6text eqs " = "
7note emit writes every byte of a to stdout; a short write is retried, an error exits 1
8fn emit a view uchar * to void
9o = ulong 0
10while lt o len a bound 4096
11k = write 1 a o sub len a o
12if le k 0
13exit 1
14end
15o = add o ulong k
16end
17end
18note emitu writes n in decimal; 20 digits hold every ulong
19fn emitu n ulong to void
20d = new uchar 20 0
21i = ulong 20
22v = n
23while true bound 20
24i = sub i 1
25put d i uchar add rem v 10 48
26v = div v 10
27if eq v 0
28break
29end
30end
31emit part d i sub 20 i
32end
33note prime factors in ascending order by trial division; 1 is shown as itself
34note bound: each pass divides v or steps d, and d * d <= v; for the inputs here d never passes 6857
35fn tell n ulong to void
36emitu n
37emit eqs
38if eq n 1
39emitu 1
40emit nl
41ret
42end
43v = n
44d = ulong 2
45f = ulong 0
46while le mul d d v bound 999999
47if eq rem v d 0
48if gt f 0
49emit x
50end
51emitu d
52f = add f 1
53v = div v d
54else
55d = add d 1
56end
57end
58if gt v 1
59if gt f 0
60emit x
61end
62emitu v
63end
64emit nl
65end
66fn main to int
67for i ulong 25
68tell add i 1
69end
70for i ulong 13
71tell add 2144 i
72end
73tell 600851475143
74tell 1000000000000
75ret 0
76end