interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / prime-decomposition / prime-decomposition.om

69 lines · 1174 bytes Raw Blame History
1note Rosetta Code task "Prime decomposition": https://rosettacode.org/wiki/Prime_decomposition
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text eqs " = "
6text times " x "
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 trial division; d stays below 2^32 for these inputs, so mul d d does not wrap
34fn factor n ulong to void
35emitu n
36emit eqs
37v = n
38d = ulong 2
39first = true
40while le mul d d v bound 999999
41if eq rem v d 0
42if not first
43emit times
44end
45first = false
46emitu d
47v = div v d
48else
49d = add d 1
50end
51end
52if gt v 1
53if not first
54emit times
55end
56emitu v
57end
58emit nl
59end
60fn main to int
61factor 12
62factor 360
63factor 97
64factor 1001
65factor 2147483647
66factor 600851475143
67factor 18446744073709551615
68ret 0
69end