Population count: Difference between revisions

Added Wren
(Added Wren)
Line 3,117:
 
Pattern:↓͢ ↑\↑͞ ↓͢ ↑͞ ↓/↓͢ ↑\↑͞ ↓/↓͢ ↑͞ ↓͢ ↑\↑͞ ↓͢ ↑͞ ↓/↓͢ ↑͞ ↓͢ ↑\↑͞ ↓/↓͢ ↑\↑͞ ↓͢ ↑͞ ↓/↓͢ ↑\↑͞ ↓/↓͢ ↑͞ ↓͢ ↑\↑͞ ↓/↓͢ ↑\↑͞ ↓͢ ↑͞ ↓/↓͢ ↑͞ ↓͢ ↑</pre>'''P.S.''', The underscore-right-arrows and overscore characters on the Pattern line may not appear properly on some browsers.
 
=={{header|Wren}}==
{{libheader|Wren-big}}
{{libheader|Wren-big}}
The first part is slightly awkward for Wren as 'native' bit-wise operations are limited to unsigned 32-bit integers and 3^21 exceeds this limit. We therefore need to switch to BigInts just before that point to process the remaining powers.
<lang ecmascript>import "/big" for BigInt
import "/fmt" for Fmt
 
var popCount = Fn.new { |n|
var count = 0
while (n != 0) {
n = n & (n - 1)
count = count + 1
}
return count
}
 
System.print("The population count of the first 30 powers of 3 is:")
var p3 = 1
for (i in 0..29) {
System.write("%(popCount.call(p3)) ")
p3 = p3 * 3
if (i == 20) p3 = BigInt.new(p3)
}
var odious = []
System.print("\n\nThe first 30 evil numbers are:")
var count = 0
var n = 0
while (count < 30) {
var pc = popCount.call(n)
if (pc%2 == 0) {
System.write("%(n) ")
count = count + 1
} else {
odious.add(n)
}
n = n + 1
}
odious.add(n)
System.print("\n\nThe first 30 odious numbers are:")
Fmt.print("$d", odious)</lang>
 
{{out}}
<pre>
The population count of the first 30 powers of 3 is:
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
 
The first 30 evil numbers are:
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
 
The first 30 odious numbers are:
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
</pre>
 
=={{header|Yabasic}}==
9,485

edits