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