# Recursively counting numbers with fixed bit counts

I ran across this problem in a reddit side-bar job-ad, and was intrigued by the task (description paraphrased to decrease googleability):

Write a function`` uint64_t bitsquares(uint64_t a, uint64_t b);``such that it return the number of integers in [a,b] that have a square number of bits set to 1. Your function should run in less than O(b-a).

I think I see how to do it in something like logarithmic time. Here's how:

First off, we notice that we can list all the squares between 0 and 64: these are 0, 1, 9, 16, 25, 36, 49, and 64. The function I will propose will run through a binary tree of depth 64, shortcutting through branches whenever it can. In fact; changing implementation language completely, I wonder if I cannot even write it comprehensively in Haskell.

The key insight I had was that whenever you try to find the number of numbers with a bitcount matching some element of some list within the bounds of 0b0000...0000 and 0b000...01111...11, then it reduces to a simple binomial coefficient -- n choose k gives the number of numbers with k bits set among the n last. Furthermore, we can reduce the total size of the problem by removing a matching prefix from the two numbers we test from.

Hence, we trace how many bits off the top agree between the two numbers. We count the set bits among these, subtract them from each representative in the list of squares, giving us the counts we need to hit in the remainder.

Write a' for a with the agreeing prefix removed, and similarly for b'.
Then the total count is the count for the reduced things from a' to
0b000...01...111 plus the count for the reduced things from 0 to b'. The
reduction count for b' needs to be 1 larger than the one for a' since in
one case, we are working with the prefix before the varying bit
increases, and in the other, we work with the prefix after the varying
bit increases -- the latter count is not *really* from 0 to b', but this
is a useful proxy for the count from 0b0000...010...000 to b' with the
additional high bit set.