Population count: Difference between revisions

Add BCPL
(Add BQN)
(Add BCPL)
Line 978:
</pre>
 
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
 
// Definitions
let popcount(n) = n=0 -> 0, (n&1) + popcount(n >> 1)
let evil(n) = (popcount(n) & 1) = 0
let odious(n) = (popcount(n) & 1) = 1
 
// The BCPL word size is implementation-dependent,
// but very unlikely to be big enough to store 3^29.
// This implements a 48-bit integer using byte strings.
let move48(dest, src) be
for i=0 to 5 do dest%i := src%i
 
let set48(dest, n) be
for i=5 to 0 by -1
$( dest%i := n & #XFF
n := n >> 8
$)
let add48(dest, src) be
$( let temp = ? and carry = 0
for i=5 to 0 by -1
$( temp := dest%i + src%i + carry
carry := temp > #XFF -> 1, 0
dest%i := temp & #XFF
$)
$)
 
let mul3(n) be
$( let temp = vec 2 // big enough even on a 16-bit machine
move48(temp, n)
add48(n, n)
add48(n, temp)
$)
 
let popcount48(n) = valof
$( let total = 0
for i=0 to 5 do
total := total + popcount(n%i)
resultis total
$)
 
// print the first N numbers
let printFirst(amt, prec) be
$( let seen = 0 and n = 0
until seen >= amt
$( if prec(n)
$( writed(n, 3)
seen := seen + 1
$)
n := n + 1
$)
wrch('*N')
$)
 
let start() be
$( let pow3 = vec 2
// print 3^0 to 3^29
set48(pow3, 1)
for i = 0 to 29
$( writed(popcount48(pow3), 3)
mul3(pow3)
$)
wrch('*N')
// print the first 30 evil and odious numbers
printFirst(30, evil)
printFirst(30, odious)
$)</lang>
{{out}}
<pre> 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
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
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|BQN}}==
<lang bqn>PopCount ← {(2|𝕩)+𝕊⍟×⌊𝕩÷2}
Line 993 ⟶ 1,068:
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|C}}==
{{works with|GCC}}
2,114

edits