interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / population-count.wiki

82 lines · 1760 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text pw "popcount of 3^0 .. 3^29:"
9text evil "first 30 evil numbers: "
10text odious "first 30 odious numbers:"
11note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12fn emit a view uchar * to void
13o = ulong 0
14while lt o len a bound 4096
15k = write 1 a o sub len a o
16if le k 0
17exit 1
18end
19o = add o ulong k
20end
21end
22note emitu writes n in decimal; 20 digits hold every ulong
23fn emitu n ulong to void
24d = new uchar 20 0
25i = ulong 20
26v = n
27while true bound 20
28i = sub i 1
29put d i uchar add rem v 10 48
30v = div v 10
31if eq v 0
32break
33end
34end
35emit part d i sub 20 i
36end
37note Kernighan: n & (n - 1) clears the lowest set bit
38fn popc n ulong to ulong
39c = ulong 0
40v = n
41while gt v 0 bound 64
42v = and v sub v 1
43c = add c 1
44end
45ret c
46end
47note evil numbers have an even population count, odious ones an odd count
48fn list want ulong to void
49c = ulong 0
50n = ulong 0
51while lt c 30 bound 100
52if eq rem popc n 2 want
53emit sp
54emitu n
55c = add c 1
56end
57n = add n 1
58end
59emit nl
60end
61fn main to int
62emit pw
63p = ulong 1
64for i ulong 30
65emit sp
66emitu popc p
67p = mul p 3
68end
69emit nl
70emit evil
71list 0
72emit odious
73list 1
74ret 0
75end
76</syntaxhighlight>
77{{out}}
78<pre>
79popcount of 3^0 .. 3^29: 1 2 2 4 3 6 6 5 6 8 9 13 10 11 14 15 11 14 14 17 17 20 19 22 16 18 24 30 25 25
80first 30 evil numbers: 0 3 5 6 9 10 12 15 17 18 20 23 24 27 29 30 33 34 36 39 40 43 45 46 48 51 53 54 57 58
81first 30 odious numbers: 1 2 4 7 8 11 13 14 16 19 21 22 25 26 28 31 32 35 37 38 41 42 44 47 49 50 52 55 56 59
82</pre>