Rebol3 Code Examplex
Population count
The number of 1s (ones) in the binary representation of a non-negative integer.
Rebol [
title: "Rosetta code: Population count"
file: %Population_count.r3
url: https://rosettacode.org/wiki/Population_count
]
pop-count: func [
"Count number of 1-bits in integer"
num [integer!]
][
count: 0
while [num > 0] [
count: count + (num % 2)
num: num // 2
]
count
]
print "population count for the first thirty powers of 3:"
print collect [for i 0 29 1 [keep pop-count to integer! 3 ** i]]
print "first thirty evil numbers:"
print copy/part collect [for i 0 299 1 [if even? pop-count i [keep i]]] 30
print "first thirty odious numbers:"
print copy/part collect [for i 0 299 1 [if odd? pop-count i [keep i]]] 30
Output:
population count for the first thirty powers of 3:
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
first thirty 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
first thirty 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