Yes, that’s right, that Challenge #12, posted one year, five months, and a day ago. You see, I have this nasty habit of starting things and not finishing them… well, better late than never!
Question two of the aforementioned challenge asked this:
Given a positive integer n, in how many ways can n be written as a sum of powers of two, when each power is allowed to occur at most twice? Say
denotes the number of different ways to write n as a sum of powers of two with at most two copies of each power allowed. What can you say about the function h?
These representations using at most two copies of each power of two are sometimes called hyperbinary representations. For example, 11 can be written as
or as
. It turns out these are the only hyperbinary representations of 11, so
.
How to analyze this? We can use a similar approach to that outlined in the solution to part III (which asked a similar question, but allowing any number of copies of each power of two). First let’s think about odd numbers, that is, numbers of the form
. Any representation of an odd number must include an odd number of copies of
(otherwise the sum won’t be odd!), but since at most two copies are allowed, there must be exactly one copy of
. If we take away the
and divide everything else by two, we get a valid representation of
. The reverse works as well: if we start with any valid representation of
, multiply everything by two, and add a copy of
, we get a valid representation of
. This means that
.
What about even numbers? A valid representation of
has either two copies of
, or none. If it has none, we can simply divide by two to get a valid representation of
(and vice versa); if two, we can take away the copies of
and then divide by two to get a representation of
, and vice versa. Therefore,
.
These two equations,
and
, along with the fact that
, are enough to completely characterize
. We can easily compute
for the first few values of
:
1,1,2,1,3,2,3,1,4,3,5,2,5,3,4,1,5,4,7,3,8,5,7,2,7,5,8,3,7,4,5,1…
It’s immediately striking, of course, that
isn’t strictly increasing: that is, just because
doesn’t mean
. In fact, even as the values of
continue to get bigger on average, there are still some values of 1 hiding in there. In fact,
, and the obvious conjecture is that
for every
.
The next thing we notice, after staring a bit, is that the
sequence contains a lot of palindromes! Every section sandwiched in between two 1’s seems to read the same forwards and backwards. Let’s make some graphs so we can see these patterns more easily: below I’ve made graphs showing
as
goes from 1 to 100, 1000, and 10000, respectively.



Pretty! We can clearly see the symmetric structure here, and the way that it occasionally (every
) jumps back down to 1.
Here’s another way to think about building the sequence. We start with the first two elements:

Now we copy the red part onto the end of the sequence (the copy will be blue), leaving a blank space in front of each blue number:

Now we fill in each blank (in green) by adding the two numbers on either side of it:

Now the blue and green part becomes the new red part:

Now we repeat! Copy the red part to the end, leaving a blank in front of each copied number:

Fill in each blank by adding the two numbers on either side:

And again: blue and green become the new reds, copy the red numbers to the end with a blank in front of each:

And fill in the blanks by adding:

And so on. It takes a little bit of thought, but it can be seen that the method I’ve described here is really just a different way of expressing the equations
and
! But thinking about it this way makes it apparent why a 1 always shows up at
, and also why we end up with palindromes.
I’ll stop there for now; in an upcoming post I’ll bring things full circle by showing how these numbers relate to the Calkin-Wilf tree of rationals.