Open to see the solution to part (c); try solving it first or see the hints above!
Given the two rules in part (a) above, we can combine them to learn something
else:
a2n+1a2n+1=an+1=a2n+1given a2n=anThus, we know that:
- an=a2n=a4n=…=a2in → shows that multiplying
n by any power of 2 has the same value of an
- a2n+1=a2n+1 → this shows that numbers ai with an odd
index i are larger by 1 than the preceding even-numbered index
Dividing by 2 is equivalent to a right-shift of the binary representation by
1 position. In binary representation, an even number is one that ends with
0 and an odd number is one which has a 1 in its last digit.
Similarly, for the odd numbers, we still right-shift by 1, but we add 1 to
the value of the even ai that we compute recursively.
With ai with odd-valued indexes i being just 1 larger than the even
number that precedes it, and with powers-of-two having 1 or more trailing
zeros in their binary representation, which can be removed by shifting right
until there’s a 1 in the last position, we can more clearly understand the
algorithm.
Overall, we keep right-shifting the number if it’s even (has a 0 as its last
digit, so nothing to save) and we add 1 to the tally if the number is odd
(its last digit is 1). Putting these steps together, we are counting the
number of bits set to 1 in the binary representation of i.
Thus, since we are given that a0=1 and 0 has no digits of 1, we can
see that:
ai=1+{number of bits set to 1 in the binary representation of i}
For notational convenience, let’s define:
N1(i)={number of bits set to 1 in the binary representation of i}
and hence, we have:
ai=1+N1(i)
Using the subscript 2 to describe binary representations, let’s validate this
with the above numbers.
a0a1a2a3a4=1+N1(0)=1+N1(02)=1+0=1=1+N1(1)=1+N1(12)=1+1=2=1+N1(2)=1+N1(102)=1+1=2=1+N1(3)=1+N1(112)=1+2=3=1+N1(4)=1+N1(1002)=1+1=2Let’s now try it for the large example above, namely a1981:
==========a19811+a9901+a4952+a2473+a1234+a615+a305+a156+a77+a38+a1===========1+N1(1981)1+1+N1(990)1+1+N1(495)2+1+N1(247)3+1+N1(123)4+1+N1(61)5+1+N1(30)5+1+N1(15)6+1+N1(7)7+1+N1(3)8+1+N1(1)===========1+N1(111101111012)1+1+N1(11110111102)1+1+N1(1111011112)2+1+N1(111101112)3+1+N1(11110112)4+1+N1(1111012)5+1+N1(111102)5+1+N1(11112)6+1+N1(1112)7+1+N1(112)8+1+N1(12)===========1+91+1+81+1+82+1+73+1+64+1+55+1+45+1+46+1+37+1+28+1+1=10=10=10=10=10=10=10=10=10=10=10What’s interesting about this is that some CPU architectures have a dedicated
instruction to counting the number of bits set to 1 in a register, this is
typically called “population count”, also referred to as Hamming
weight and is used in chess
engines, among many other use cases.
Let’s prove this solution inductively#
We can prove that this is the correct formula by induction. Let’s recall the
formula we’re trying to prove:
an=1+{number of bits set to 1 in the binary representation of n}
And we need to prove that given the general rule for computing ai above, the following is true:
a2na2n+1=an=an+1(1)(2)Let’s consider the base case where n=0:
a2na2⋅0a0=an=a0=a0(1)substitute n=0The first statement is trivially true. The second statement evaluates to:
a2n+1a2⋅0+1a11+N1(1)1+12=an+1=a0+1=a0+1=1+N1(0)+1=1+0+1=2(2)substitute n=0Which is also true.
Now, let’s assume this is true for an and prove it for an+1. We have to
consider two separate cases: one where n is even, and one where n is odd.
Inductive case when n is even#
Let’s first consider the case where n is even. If n is even, that means we
can find k such that n=2k, and we have:
a2ia2(n+1)a2n+21+N1(2n+2)N1(2n+2)=ai=an+1=an+1=1+N1(n+1)=N1(n+1)(1) with substituting n→isubstitute i=n+1simplifyuse formulasubtract 1 from each side (E1)Since n is even, we know that 2n is divisible by 4, so its binary
representation is the same as n but shifted over one digit, and it has 2
trailing zeroes: …00. Thus, when we add 2 to n, we are just flipping
the second-to-last 0 to 1, and hence, increasing the number of ones in the
binary representation by 1 (without affecting any other digits). Thus:
N1(2n+2)=N1(2n)+1
Since n is even, we know that 2n is just shifting the binary representation
over by one digit, but not changing the number of ones, so using
N1(2n)=N1(n), thus:
N1(2n+2)=N1(2n)+1=N1(n)+1
Additionally, since n is even, its last digit must be 0 so n+1 is simply
flipping the last 0 digit to 1 without affecting any other digits, and
hence, increasing its count of ones by 1. This means that:
N1(n+1)=N1(n)+1
And now we can prove the above statement (E1):
N1(2n+2)=N1(2n)+1=N1(n)+1=N1(n+1)
Let’s now consider equation (2):
a2k+1a2(n+1)+1a2n+31+N1(2n+3)N1(2n+3)=ak+1=an+1+1=an+1+1=1+N1(n+1)+1=N1(n+1)+1(2) with substituting n→ksubstitute k=n+1simplifyevaluate aisubtract 1 from each side (E2)Since n is even, 2n is divisible by 4, so the last two digits in the
binary representation are 0: …002. 3 in binary is 112, so adding
3 to a number increases its count of ones by 2:
N1(2n+3)=N1(2n)+2
Additionally, we also know that:
N1(2n)=N1(n)
since the number of ones in a number doesn’t change when multiplying by 2, and hence:
N1(2n+3)=N1(2n)+2=N1(n)+2
Similarly, since the binary representation of 1 in binary is 12, adding
this to n (which is even, and hence divisible by 2), is just flipping the
last digit from 0 to 1 and hence increasing its count of ones by 1:
N1(n+1)=N1(n)+1
Thus, we can prove statement (E2) as follows:
N1(2n+3)=N1(2n)+2=N1(n)+2=[N1(n)+1]+1=N1(n+1)+1This proves both equations when n is even. We next consider the same
equations when n is odd.
Inductive case when n is odd#
Let’s consider equation (1) when n is odd:
a2ia2(n+1)a2n+21+N1(2n+2)N1(2n+2)=ai=an+1=an+1=1+N1(n+1)=N1(n+1)(1) with substituting n→isubstitute i=n+1simplifyuse formulasubtract 1 from each side (O1)In this case, since n is odd, we know that its binary representation must end
in 1: …12. Thus, when adding 1 to it, there will be carryovers and
some 1s will turn to 0s and vice versa. We don’t know what value n has,
so we can’t make a specific argument as to its value, or where the 1s are
located.
However, the interesting thing is that 2n has the same shape in terms of
where the 0s and 1s are, they’re just shifted over by 1 position to the left. Then,
when we add 2 to 2n, we’re performing the same set of bit flips and
carryovers as when adding 1 to n.
Thus, the number of 1s in 2n+2 will the same as in n+1, and hence:
N1(2n+2)=N1(n+1)
which proves (O1).
Let’s now consider the equation (2) when n is odd:
a2k+1a2(n+1)+1a2n+31+N1(2n+3)N1(2n+3)=ak+1=an+1+1=an+1+1=1+N1(n+1)+1=N1(n+1)+1(2) with substituting n→ksubstitute k=n+1simplifyevaluate aisubtract 1 from each side (O2)Even though n is odd, 2n is even, and it’s the same bit pattern as n,
just shifted over 1 position to the left, so it ends with: …102. Then,
adding 3 to it is doing both of the following:
- flipping the last digit from 0 to 1, increasing the number of ones by
1
- flipping the second-to-last digit from 1 to 0 and carrying over the
cascading change to the left
The first change is just adding 1 to the total count. The second change is
equivalent to adding 1 to the value n, which means that:
N1(2n+3)=N1(n+1)+1
which is exactly what we needed to show to prove statement (O2).
This completes the proof. ■