interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / attractive-numbers / attractive-numbers.om

85 lines · 1568 bytes Raw Blame History
1note Rosetta Code task "Attractive numbers": https://rosettacode.org/wiki/Attractive_numbers
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text hdr "attractive numbers up to 120 (the count of prime factors, with multiplicity, is prime):\n"
6note emit writes every byte of a to stdout; a short write is retried, an error exits 1
7fn emit a view uchar * to void
8o = ulong 0
9while lt o len a bound 4096
10k = write 1 a o sub len a o
11if le k 0
12exit 1
13end
14o = add o ulong k
15end
16end
17note emitu writes n in decimal; 20 digits hold every ulong
18fn emitu n ulong to void
19d = new uchar 20 0
20i = ulong 20
21v = n
22while true bound 20
23i = sub i 1
24put d i uchar add rem v 10 48
25v = div v 10
26if eq v 0
27break
28end
29end
30emit part d i sub 20 i
31end
32note emitw writes n in decimal right-aligned in w columns (w at most 20)
33fn emitw n ulong w ulong to void
34d = new uchar 20 32
35i = ulong 20
36v = n
37while true bound 20
38i = sub i 1
39put d i uchar add rem v 10 48
40v = div v 10
41if eq v 0
42break
43end
44end
45if gt i sub 20 w
46i = sub 20 w
47end
48emit part d i sub 20 i
49end
50note Omega(n): the number of prime factors of n counted with multiplicity
51fn bigo n ulong to ulong
52v = n
53d = ulong 2
54k = ulong 0
55while le mul d d v bound 64
56if eq rem v d 0
57k = add k 1
58v = div v d
59else
60d = add d 1
61end
62end
63if gt v 1
64k = add k 1
65end
66ret k
67end
68note Omega(n) <= 6 for n <= 120, so the primes to test are 2, 3 and 5
69fn main to int
70emit hdr
71c = ulong 0
72for i ulong 120
73n = add i 1
74k = bigo n
75if or or eq k 2 eq k 3 eq k 5
76emitw n 4
77c = add c 1
78if eq rem c 20 0
79emit nl
80end
81end
82end
83emit nl
84ret 0
85end