Back to problem set

Disclaimer: these are my personal solutions; they have not been reviewed, so be careful relying on them as there might be errors. Some competitions may have official solutions from the contest publishers which you may wish to refer to instead if you’re looking for verified solutions.

Problem 7

Let A={a0,a1,}A = \lbrace a_0, a_1, … \rbrace be a sequence of real numbers and define the sequence A=a0,a1,A' = {a_0', a_1', …} as follows for n=0,1,n = 0, 1, …: a2n=ana_{2n}' = a_n, a2n+1=an+1a_{2n + 1}' = a_n + 1. If a0=1a_0 = 1 and A=AA' = A, find

  1. a1,a2,a3a_1, a_2, a_3 and a4a_4
  2. a1981a_{1981}
  3. A simple general algorithm for evaluating ana_n, for n=0,1,n = 0, 1, …

Hints

Hint for (a)

You can compute this with just the information given in the problem statement.

Hint for (b)

Did you get the pattern from (a)? You can just apply it for a number of steps to reach the answer.

Hint for (c)

To solve the general case, you need to find a pattern. Note that solutions to parts (a) and (b) ought to give you a hint as to what this might be. See if you can form a hypothesis and then use induction to prove it.

Solution

Open to see the solution to part (a); try solving it first or see the hints above!

Since A=AA' = A (given), we also know that ai=aia'_i = a_i for all ii, and therefore, we can also conclude that:

a2n=ana2n+1=an+1 \begin{align*} a_{2n} & = a_n \\ a_{2n+1} & = a_n + 1 \\ \end{align*}

Another way to formuate this would be:

ai={ai2 if i is evenai12+1 if i is odd \begin{equation*} a_i = \begin{cases} a_{\frac{i}{2}} & \text{ if } i \text{ is even} \\ a_{\frac{i-1}{2}} + 1 & \text{ if } i \text{ is odd} \\ \end{cases} \end{equation*}

Here’s yet another alternative:

a2i+k={ai if k=0ai+1 if k=1wherek{0,1} \begin{equation*} a_{2i + k} = \begin{cases} a_{i} & \text{ if } k = 0 \\ a_{i} + 1 & \text{ if } k = 1 \\ \end{cases} \quad \text{where} \quad k \in \lbrace 0, 1 \rbrace \end{equation*}

Let’s now compute a0,,a4a_0, …, a_4:

a0=1givena1=a0+1=2using above equationsa2=a1=2a3=a1+1=3a4=a2=a1=2 \begin{align*} a_0 & = 1 & \quad \text{given} \\ a_1 & = a_0 + 1 = \boxed{2} & \quad \text{using above equations} \\ a_2 & = a_1 = \boxed{2} \\ a_3 & = a_1 + 1 = \boxed{3} \\ a_4 & = a_2 = a_1 = \boxed{2} \\ \end{align*}
Open to see the solution to part (b); try solving it first or see the hints above!

First, read the introduction and solution to (a) above. Given the pattern, we can solve for a1981a_{1981}:

a1981=a19802+1=a990+1=a9902+1=a495+1=a4942+1+1=a247+2=a2462+1+2=a123+3=a1222+1+3=a61+4=a602+1+4=a30+5=a302+5=a15+5=a142+1+5=a7+6=a62+1+6=a3+7=a22+1+7=a1+8=2+8=10 \begin{align*} a_{1981} & = a_{\frac{1980}{2}} + 1 = a_{990} + 1 \\ & = a_{\frac {990}{2}} + 1 = a_{495} + 1 \\ & = a_{\frac {494}{2}} + 1 + 1 = a_{247} + 2 \\ & = a_{\frac {246}{2}} + 1 + 2 = a_{123} + 3 \\ & = a_{\frac {122}{2}} + 1 + 3 = a_{61} + 4 \\ & = a_{\frac {60}{2}} + 1 + 4 = a_{30} + 5 \\ & = a_{\frac {30}{2}} + 5 = a_{15} + 5 \\ & = a_{\frac {14}{2}} + 1 + 5 = a_{7} + 6 \\ & = a_{\frac {6}{2}} + 1 + 6 = a_{3} + 7 \\ & = a_{\frac {2}{2}} + 1 + 7 = a_{1} + 8 \\ & = 2 + 8 = \boxed{10} \\ \end{align*}
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+1=an+1a2n+1=a2n+1given a2n=an \begin{align*} a_{2n+1} & = a_n + 1 & \\ a_{2n+1} & = a_{2n} + 1 & \qquad \textrm{given $a_{2n} = a_n$} \\ \end{align*}

Thus, we know that:

  • an=a2n=a4n==a2ina_n = a_{2n} = a_{4n} = … = a_{2^i n} → shows that multiplying nn by any power of 22 has the same value of ana_n
  • a2n+1=a2n+1a_{2n+1} = a_{2n} + 1 → this shows that numbers aia_i with an odd index ii are larger by 11 than the preceding even-numbered index

Dividing by 22 is equivalent to a right-shift of the binary representation by 11 position. In binary representation, an even number is one that ends with 00 and an odd number is one which has a 11 in its last digit.

Similarly, for the odd numbers, we still right-shift by 11, but we add 11 to the value of the even aia_i that we compute recursively.

With aia_i with odd-valued indexes ii being just 11 larger than the even number that precedes it, and with powers-of-two having 11 or more trailing zeros in their binary representation, which can be removed by shifting right until there’s a 11 in the last position, we can more clearly understand the algorithm.

Overall, we keep right-shifting the number if it’s even (has a 00 as its last digit, so nothing to save) and we add 11 to the tally if the number is odd (its last digit is 11). Putting these steps together, we are counting the number of bits set to 11 in the binary representation of ii.

Thus, since we are given that a0=1a_0 = 1 and 00 has no digits of 11, we can see that:

ai=1+{number of bits set to 1 in the binary representation of i} a_i = 1 + \lbrace \text{number of bits set to $1$ in the binary representation of $i$} \rbrace

For notational convenience, let’s define:

N1(i)={number of bits set to 1 in the binary representation of i} \mathbf{N_1}(i) = \lbrace \text{number of bits set to 1 in the binary representation of $i$} \rbrace

and hence, we have:

ai=1+N1(i) \boxed{a_i = 1 + \mathbf{N_1}(i)}

Using the subscript 22 to describe binary representations, let’s validate this with the above numbers.

a0=1+N1(0)=1+N1(02)=1+0=1a1=1+N1(1)=1+N1(12)=1+1=2a2=1+N1(2)=1+N1(102)=1+1=2a3=1+N1(3)=1+N1(112)=1+2=3a4=1+N1(4)=1+N1(1002)=1+1=2 \begin{align*} a_0 & = 1 + \mathbf{N_1}(0) = 1 + \mathbf{N_1}(0_2) = 1 + 0 = 1 \\ a_1 & = 1 + \mathbf{N_1}(1) = 1 + \mathbf{N_1}(1_2) = 1 + 1 = 2 \\ a_2 & = 1 + \mathbf{N_1}(2) = 1 + \mathbf{N_1}(10_2) = 1 + 1 = 2 \\ a_3 & = 1 + \mathbf{N_1}(3) = 1 + \mathbf{N_1}(11_2) = 1 + 2 = 3 \\ a_4 & = 1 + \mathbf{N_1}(4) = 1 + \mathbf{N_1}(100_2) = 1 + 1 = 2 \\ \end{align*}

Let’s now try it for the large example above, namely a1981a_{1981}:

a1981=1+N1(1981)=1+N1(111101111012)=1+9=10=1+a990=1+1+N1(990)=1+1+N1(11110111102)=1+1+8=10=1+a495=1+1+N1(495)=1+1+N1(1111011112)=1+1+8=10=2+a247=2+1+N1(247)=2+1+N1(111101112)=2+1+7=10=3+a123=3+1+N1(123)=3+1+N1(11110112)=3+1+6=10=4+a61=4+1+N1(61)=4+1+N1(1111012)=4+1+5=10=5+a30=5+1+N1(30)=5+1+N1(111102)=5+1+4=10=5+a15=5+1+N1(15)=5+1+N1(11112)=5+1+4=10=6+a7=6+1+N1(7)=6+1+N1(1112)=6+1+3=10=7+a3=7+1+N1(3)=7+1+N1(112)=7+1+2=10=8+a1=8+1+N1(1)=8+1+N1(12)=8+1+1=10 \begin{align*} & & a_{1981} & = & 1 + \mathbf{N_1}(1981) & = & 1 + \mathbf{N_1}(11110111101_2) & = & 1 + 9 & = 10 \\ & = & 1 + a_{990} & = & 1 + 1 + \mathbf{N_1}(990) & = & 1 + 1 + \mathbf{N_1}(1111011110_2) & = & 1 + 1 + 8 & = 10 \\ & = & 1 + a_{495} & = & 1 + 1 + \mathbf{N_1}(495) & = & 1 + 1 + \mathbf{N_1}(111101111_2) & = & 1 + 1 + 8 & = 10 \\ & = & 2 + a_{247} & = & 2 + 1 + \mathbf{N_1}(247) & = & 2 + 1 + \mathbf{N_1}(11110111_2) & = & 2 + 1 + 7 & = 10 \\ & = & 3 + a_{123} & = & 3 + 1 + \mathbf{N_1}(123) & = & 3 + 1 + \mathbf{N_1}(1111011_2) & = & 3 + 1 + 6 & = 10 \\ & = & 4 + a_{61} & = & 4 + 1 + \mathbf{N_1}(61) & = & 4 + 1 + \mathbf{N_1}(111101_2) & = & 4 + 1 + 5 & = 10 \\ & = & 5 + a_{30} & = & 5 + 1 + \mathbf{N_1}(30) & = & 5 + 1 + \mathbf{N_1}(11110_2) & = & 5 + 1 + 4 & = 10 \\ & = & 5 + a_{15} & = & 5 + 1 + \mathbf{N_1}(15) & = & 5 + 1 + \mathbf{N_1}(1111_2) & = & 5 + 1 + 4 & = 10 \\ & = & 6 + a_7 & = & 6 + 1 + \mathbf{N_1}(7) & = & 6 + 1 + \mathbf{N_1}(111_2) & = & 6 + 1 + 3 & = 10 \\ & = & 7 + a_3 & = & 7 + 1 + \mathbf{N_1}(3) & = & 7 + 1 + \mathbf{N_1}(11_2) & = & 7 + 1 + 2 & = 10 \\ & = & 8 + a_1 & = & 8 + 1 + \mathbf{N_1}(1) & = & 8 + 1 + \mathbf{N_1}(1_2) & = & 8 + 1 + 1 & = 10 \\ \end{align*}

What’s interesting about this is that some CPU architectures have a dedicated instruction to counting the number of bits set to 11 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} a_n = 1 + \lbrace \text{number of bits set to $1$ in the binary representation of $n$} \rbrace

And we need to prove that given the general rule for computing aia_i above, the following is true:

a2n=an(1)a2n+1=an+1(2) \begin{align*} a_{2n} & = a_n & \qquad \textrm{(1)} \\ a_{2n+1} & = a_n + 1 & \qquad \textrm{(2)} \\ \end{align*}

Let’s consider the base case where n=0n = 0:

a2n=an(1)a20=a0substitute n=0a0=a0 \begin{align*} a_{2n} & = a_n & \qquad \textrm{(1)} \\ a_{2 \cdot 0} & = a_0 & \qquad \textrm{substitute $n = 0$} \\ a_0 & = a_0 & \\ \end{align*}

The first statement is trivially true. The second statement evaluates to:

a2n+1=an+1(2)a20+1=a0+1substitute n=0a1=a0+11+N1(1)=1+N1(0)+11+1=1+0+12=2 \begin{align*} a_{2n+1} & = a_n + 1 & \qquad \textrm{(2)} \\ a_{2 \cdot 0 + 1} & = a_0 + 1 & \qquad \textrm{substitute $n = 0$} \\ a_1 & = a_0 + 1 & \\ 1 + \mathbf{N_1}(1) & = 1+ \mathbf{N_1}(0) + 1 & \\ 1 + 1 & = 1 + 0 + 1 & \\ 2 & = 2 & \\ \end{align*}

Which is also true.

Now, let’s assume this is true for ana_n and prove it for an+1a_{n+1}. We have to consider two separate cases: one where nn is even, and one where nn is odd.

Inductive case when nn is even

Let’s first consider the case where nn is even. If nn is even, that means we can find kk such that n=2kn = 2k, and we have:

a2i=ai(1) with substituting nia2(n+1)=an+1substitute i=n+1a2n+2=an+1simplify1+N1(2n+2)=1+N1(n+1)use formulaN1(2n+2)=N1(n+1)subtract 1 from each side (E1) \begin{align*} a_{2i} & = a_i & \qquad \textrm{(1) with substituting $n \rarr i$} \\ a_{2(n+1)} & = a_{n+1} & \qquad \textrm{substitute $i = n+1$} \\ a_{2n+2} & = a_{n+1} & \qquad \textrm{simplify} \\ 1 + \mathbf{N_1}(2n+2) & = 1 + \mathbf{N_1}(n+1) & \qquad \textrm{use formula} \\ \mathbf{N_1}(2n+2) & = \mathbf{N_1}(n+1) & \qquad \textrm{subtract $1$ from each side $(E_1)$} \\ \end{align*}

Since nn is even, we know that 2n2n is divisible by 44, so its binary representation is the same as nn but shifted over one digit, and it has 2 trailing zeroes: 00…00. Thus, when we add 22 to nn, we are just flipping the second-to-last 00 to 11, and hence, increasing the number of ones in the binary representation by 11 (without affecting any other digits). Thus:

N1(2n+2)=N1(2n)+1 \mathbf{N_1}(2n+2) = \mathbf{N_1}(2n) + 1

Since nn is even, we know that 2n2n is just shifting the binary representation over by one digit, but not changing the number of ones, so using N1(2n)=N1(n)\mathbf{N_1}(2n) = \mathbf{N_1}(n), thus:

N1(2n+2)=N1(2n)+1=N1(n)+1 \mathbf{N_1}(2n+2) = \mathbf{N_1}(2n) + 1 = \mathbf{N_1}(n) + 1

Additionally, since nn is even, its last digit must be 00 so n+1n+1 is simply flipping the last 00 digit to 11 without affecting any other digits, and hence, increasing its count of ones by 11. This means that:

N1(n+1)=N1(n)+1 \mathbf{N_1}(n+1) = \mathbf{N_1}(n) + 1

And now we can prove the above statement (E1)(E_1):

N1(2n+2)=N1(2n)+1=N1(n)+1=N1(n+1) \mathbf{N_1}(2n+2) = \mathbf{N_1}(2n) + 1 = \mathbf{N_1}(n) + 1 = \mathbf{N_1}(n+1)

Let’s now consider equation (2)(2):

a2k+1=ak+1(2) with substituting nka2(n+1)+1=an+1+1substitute k=n+1a2n+3=an+1+1simplify1+N1(2n+3)=1+N1(n+1)+1evaluate aiN1(2n+3)=N1(n+1)+1subtract 1 from each side (E2) \begin{align*} a_{2k+1} & = a_k + 1 & \qquad \textrm{(2) with substituting $n \rarr k$} \\ a_{2(n+1) + 1} & = a_{n + 1} + 1 & \qquad \textrm{substitute $k = n + 1$} \\ a_{2n + 3} & = a_{n + 1} + 1 & \qquad \textrm{simplify} \\ 1 + \mathbf{N_1}(2n + 3) & = 1 + \mathbf{N_1}(n + 1) + 1 & \qquad \textrm{evaluate $a_i$} \\ \mathbf{N_1}(2n + 3) & = \mathbf{N_1}(n + 1) + 1 & \qquad \textrm{subtract $1$ from each side $(E_2)$} \\ \end{align*}

Since nn is even, 2n2n is divisible by 44, so the last two digits in the binary representation are 00: 002…00_2. 33 in binary is 11211_2, so adding 33 to a number increases its count of ones by 22:

N1(2n+3)=N1(2n)+2 \mathbf{N_1}(2n + 3) = \mathbf{N_1}(2n) + 2

Additionally, we also know that:

N1(2n)=N1(n) \mathbf{N_1}(2n) = \mathbf{N_1}(n)

since the number of ones in a number doesn’t change when multiplying by 22, and hence:

N1(2n+3)=N1(2n)+2=N1(n)+2 \mathbf{N_1}(2n + 3) = \mathbf{N_1}(2n) + 2 = \mathbf{N_1}(n) + 2

Similarly, since the binary representation of 11 in binary is 121_2, adding this to nn (which is even, and hence divisible by 22), is just flipping the last digit from 00 to 11 and hence increasing its count of ones by 11:

N1(n+1)=N1(n)+1 \mathbf{N_1}(n + 1) = \mathbf{N_1}(n) + 1

Thus, we can prove statement (E2)(E_2) as follows:

N1(2n+3)=N1(2n)+2=N1(n)+2=[N1(n)+1]+1=N1(n+1)+1 \begin{align*} \mathbf{N_1}(2n + 3) = \mathbf{N_1}(2n) + 2 = \mathbf{N_1}(n) + 2 = \left[ \mathbf{N_1}(n) + 1 \right] + 1 = \mathbf{N_1}(n + 1) + 1 \end{align*}

This proves both equations when nn is even. We next consider the same equations when nn is odd.

Inductive case when nn is odd

Let’s consider equation (1)(1) when nn is odd:

a2i=ai(1) with substituting nia2(n+1)=an+1substitute i=n+1a2n+2=an+1simplify1+N1(2n+2)=1+N1(n+1)use formulaN1(2n+2)=N1(n+1)subtract 1 from each side (O1) \begin{align*} a_{2i} & = a_i & \qquad \textrm{(1) with substituting $n \rarr i$} \\ a_{2(n+1)} & = a_{n+1} & \qquad \textrm{substitute $i = n+1$} \\ a_{2n+2} & = a_{n+1} & \qquad \textrm{simplify} \\ 1 + \mathbf{N_1}(2n+2) & = 1 + \mathbf{N_1}(n+1) & \qquad \textrm{use formula} \\ \mathbf{N_1}(2n+2) & = \mathbf{N_1}(n+1) & \qquad \textrm{subtract $1$ from each side $(O_1)$} \\ \end{align*}

In this case, since nn is odd, we know that its binary representation must end in 11: 12…1_2. Thus, when adding 11 to it, there will be carryovers and some 11s will turn to 00s and vice versa. We don’t know what value nn has, so we can’t make a specific argument as to its value, or where the 11s are located.

However, the interesting thing is that 2n2n has the same shape in terms of where the 00s and 11s are, they’re just shifted over by 11 position to the left. Then, when we add 22 to 2n2n, we’re performing the same set of bit flips and carryovers as when adding 11 to nn.

Thus, the number of 11s in 2n+22n+2 will the same as in n+1n+1, and hence:

N1(2n+2)=N1(n+1) \mathbf{N_1}(2n+2) = \mathbf{N_1}(n+1)

which proves (O1)(O_1).

Let’s now consider the equation (2)(2) when nn is odd:

a2k+1=ak+1(2) with substituting nka2(n+1)+1=an+1+1substitute k=n+1a2n+3=an+1+1simplify1+N1(2n+3)=1+N1(n+1)+1evaluate aiN1(2n+3)=N1(n+1)+1subtract 1 from each side (O2) \begin{align*} a_{2k+1} & = a_{k} + 1 & \qquad \textrm{(2) with substituting $n \rarr k$} \\ a_{2(n+1) + 1} & = a_{n+1} + 1 & \qquad \textrm{substitute $k = n + 1$} \\ a_{2n + 3} & = a_{n + 1} + 1 & \qquad \textrm{simplify} \\ 1 + \mathbf{N_1}(2n + 3) & = 1 + \mathbf{N_1}(n + 1) + 1 & \qquad \textrm{evaluate $a_i$} \\ \mathbf{N_1}(2n + 3) & = \mathbf{N_1}(n + 1) + 1 & \qquad \textrm{subtract $1$ from each side $(O_2)$} \\ \end{align*}

Even though nn is odd, 2n2n is even, and it’s the same bit pattern as nn, just shifted over 11 position to the left, so it ends with: 102…10_2. Then, adding 33 to it is doing both of the following:

  1. flipping the last digit from 00 to 11, increasing the number of ones by 11
  2. flipping the second-to-last digit from 11 to 00 and carrying over the cascading change to the left

The first change is just adding 11 to the total count. The second change is equivalent to adding 11 to the value nn, which means that:

N1(2n+3)=N1(n+1)+1 \mathbf{N_1}(2n + 3) = \mathbf{N_1}(n + 1) + 1

which is exactly what we needed to show to prove statement (O2)(O_2).

This completes the proof.    \ \ \blacksquare