Population count: Difference between revisions

Line 2,929:
[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]
>>> </lang>
 
===Python: Kernighans' algorithm===
The algorithm is explained [https://www.techiedelight.com/brian-kernighans-algorithm-count-set-bits-integer/ here]. Replace popcount with pop_kernighan in the example above to get the same evil and odious results.
 
<lang python>def pop_kernighan(n):
i = 0
while n:
i, n = i + 1, n & (n - 1)
return i
</lang>
 
===Composition of pure functions===
Anonymous user