interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / population-count / population-count.om

73 lines · 1308 bytes Raw Blame History
1note Rosetta Code task "Population count": https://rosettacode.org/wiki/Population_count
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text pw "popcount of 3^0 .. 3^29:"
7text evil "first 30 evil numbers: "
8text odious "first 30 odious numbers:"
9note emit writes every byte of a to stdout; a short write is retried, an error exits 1
10fn emit a view uchar * to void
11o = ulong 0
12while lt o len a bound 4096
13k = write 1 a o sub len a o
14if le k 0
15exit 1
16end
17o = add o ulong k
18end
19end
20note emitu writes n in decimal; 20 digits hold every ulong
21fn emitu n ulong to void
22d = new uchar 20 0
23i = ulong 20
24v = n
25while true bound 20
26i = sub i 1
27put d i uchar add rem v 10 48
28v = div v 10
29if eq v 0
30break
31end
32end
33emit part d i sub 20 i
34end
35note Kernighan: n & (n - 1) clears the lowest set bit
36fn popc n ulong to ulong
37c = ulong 0
38v = n
39while gt v 0 bound 64
40v = and v sub v 1
41c = add c 1
42end
43ret c
44end
45note evil numbers have an even population count, odious ones an odd count
46fn list want ulong to void
47c = ulong 0
48n = ulong 0
49while lt c 30 bound 100
50if eq rem popc n 2 want
51emit sp
52emitu n
53c = add c 1
54end
55n = add n 1
56end
57emit nl
58end
59fn main to int
60emit pw
61p = ulong 1
62for i ulong 30
63emit sp
64emitu popc p
65p = mul p 3
66end
67emit nl
68emit evil
69list 0
70emit odious
71list 1
72ret 0
73end