Monday, June 23, 2014

Sum of Three Squares and Factors of Two

Background

I've been working a lot recently with the sums of squares (I have a previous post about finding the Exhaustive List of the Sums of Four-Squares). This work has been for some research and also for Project Euler. However, I came across an interesting result: If $n$ is even and $a^2 + b^2 + c^2 = n^2$, then $a,b,$ and $c$ are also even.

To kind of tip-toe around the issue, we know that there is at least one solution $a,b,c$ such that this is true: $a=n, b=c=0$. Others may and do exist, but we won't fret too much right now about what they are. All we need to know is that there is at least one solution. Since all natural numbers are either even or odd, we have four possibilities: all $a,b,c$ are odd, two are odd and one is even, one is odd and two are even, or all three are even.

We can easily rule out the case for which one is odd and three are odd. Because we know $n$ is even, $n^2$ must also be even. However, there are an odd number of odds on the other side (remember an odd squared is still odd), which means the entire other side is odd. And, since an odd cannot equal an even, we have a contradiction. However, in order to eliminate the case where two are odd, we must do a little more work.

Assume, without loss of generality, that $a$ and $b$ are odd and $c$ is even and we will prove a contradiction. Let $d,e,f,g\in\mathbb{N}$ be such that $$\begin{array}{c} a = 2d + 1 \\ b = 2e + 1 \\ c = 2f \\ n = 2g \end{array} $$ We know that. $$n^2 = (2g)^2 = 4g^2$$ And we know that $$\begin{array}{r l} n^2 &= a^2 + b^2 + c^2 \\ &= (2d+1)^2 + (2e+1)^2 + (2f)^2 \\ & = 4d^2 + 4d + 1 + 4e^2 + 4e + 1 + 4f^2 \\ &= 4(d^2 + d + e^2 + e + f^2) + 2 \end{array}$$ Putting the two together: $$4g^2 = 4(d^2 + d + e^2 + e + f^2) + 2$$ Clearly, the left hand side is a multiple of $4$, however, the righthand side is not. Therefore, these two cannot be equal and we cannot have two odd squares to sum. We won't try to eliminate the last possibility (that all three be even) because we know that at least one solution exists and it has to fall into one of those categories.

Now, this probably doesn't seem like that interesting of a result. And honestly speaking, it's really not particularly interesting. However, we can generalizes this:

Theorem


If $2^k\vert n$ for some $k\in\mathbb{N}$ and $a^2 + b^2 + c^2 = n^2$ for $a,b,c\in \mathbb{N}$, then $2^k\vert a,b,c$.


Proof

For $k=1$, this simplifies to the above proof and $k=0$ is trivial. As such, we have base cases and we will proceed by induction. So, we assume that if $2^{k-1}\vert n$ for some $k\in \mathbb{N}$ and $a^2 + b^2 + c^2 = n^2$, then $2^{k-1}\vert a,b,c$. Now assume that $2^k\vert n$ and that $a^2 + b^2 + c^2 = n^2$. We know that there exists $m$ such that $n=2^km$. We also can show that $$n = 2^km = 2^{k-1}(2m)$$ which means that $2^{k-1}\vert n$. And, since $a^2 + b^2 + c^2 = n^2$ by our induction hypothesis, we can conclude that $2^{k-1}\vert a,b,c$. This implies there exists $i,j,l\in \mathbb{N}$ such that $$\begin{array}{c} a = 2^{k-1}i \\ b = 2^{k-1}j \\ c = 2^{k-1}l \\ \end{array}$$ We can now classify $a,b,c$ based on the respective parity of $i,j,l$ (because again, $i,j,l$ must either be even or odd). We, again, have four possibilities: all three are odd, two are odd and one is even, one is odd and two are even, and all three are even.

Case 1

Assume that all three are odd. Therefore, there exists $i_0, j_0, l_0$ such that $$\begin{array}{c} i = 2i_0 + 1 \\ j = 2j_0 + 1 \\ l = 2l_0 + 1 \end{array}$$ and furthermore $$\begin{array}{c} a = 2^{k-1}(2i_0 + 1) = 2^ki_0 + 2^{k-1} \\ b = 2^{k-1}(2j_0 + 1) = 2^kj_0 + 2^{k-1} \\ c = 2^{k-1}(2l_0 + 1) = 2^kl_0 + 2^{k-1} \\ \end{array}$$ Consider $$\begin{array}{r l} n^2 &= a^2 + b^2 + c^2 \\ & = (2^ki_0 + 2^{k-1})^2 + (2^kj_0 + 2^{k-1})^2 + (2^kl_0 + 2^{k-1})^2 \\ &= 2^{2k}(i_0^2+i_0+j_0^2+j_0+l_0^2+l_0) + 3(2^{2k-2}) \\ \end{array}$$ Note that this is not divisible by $2^{2k}$ because of the last term. On the contrary, note that $$n^2 = (2^km)^2 = 2^{2k}m^2$$ and therefore $2^{2k}\vert n^2$; a contradiction. Therefore, all three cannot be odd.

Case 2

Assume that two are odd and one is even. Therefore there exists $i_0,j_0, l_0$ such that $$\begin{array}{c} i = 2i_0 + 1 \\ j = 2j_0 + 1 \\ l = 2l_0 \end{array}$$ As before, we can substitute and write $a,b,c$ in terms of $i_0, j_0,l_0$ and as a result $n^2$ in terms of $i_0, j_0,l_0$. $$n^2 = 2^{2k}(i_0^2+i_0+j_0^2+j_0+l_0^2) + 2(2^{2k-2})$$ Note that this is not divisible by $2^{2k}$ because of the last term only having a factor of $2^{2k-1}$. But again, because know that $2^{2k}\vert n^2$ we have a contradiction. Therefore, two cannot be odd.

Case 3

Assume that one is odd and two are even. Therefore there exists $i_0,j_0, l_0$ such that $$\begin{array}{c} i = 2i_0 + 1 \\ j = 2j_0 \\ l = 2l_0 \end{array}$$ Once again, we can substitute and write $n^2$ in terms of $i_0, j_0,l_0$. $$n^2 = 2^{2k}(i_0^2+i_0+j_0^2+l_0^2) + 2^{2k-2}$$ Note that this is not divisible by $2^{2k}$ because of the last term. And again, because know that $2^{2k}\vert n^2$ we have a contradiction. Therefore, one cannot be odd.

Case 4

That only leaves the option that all three are even. Since $i,j,k$ are even, and we have $i_0,j_0, l_0$ such that $$\begin{array}{c} i = 2i_0 \\ j = 2j_0 \\ l = 2l_0 \end{array}$$ we can now write $$\begin{array}{c} a = 2^{k-1}(2i_0) = 2^ki_0 \\ b = 2^{k-1}(2j_0) = 2^kj_0 \\ c = 2^{k-1}(2l_0) = 2^kl_0 \\ \end{array}$$ and we have shown that $2^k\vert a,b,c$!

Implications

What does this mean, though? It means that if we want to find the three integers whose squares sum to $n^2$(such as lattice points on a sphere), then we can omit multiples of two from our search and work with smaller numbers. This saves a great deal of computing time.

For instance, if we know that $2^k\vert n$ and we want to find $a,b,c$ such that $n^2 = a^2+b^2+c^2$, we can really just search for $d,e,f$ such that $m^2=d^2+e^2+f^2$ ($n = 2^km$). We jump back to the original problem by multiplying by $2^{2k}$ on both sides: $$\begin{array}{r l} m^2 & = d^2 + e^2 + f^2\\ 2^{2k}m^2 & = 2^{2k}(d^2 + e^2 + f^2) \\ (2^km)^2&= 2^{2k}d^2 + 2^{2k}e^2 + 2^{2k}f^2 \\ n^2 & = (2^kd)^2 + (2^ke)^2 + (2^kf)^2\\ n^2 & = a^2 + b^2 + c^2 \end{array} $$ Where clearly $d,e,f$ is the respective result of the division of $a,b,c$ by $2^k$.

Monday, June 9, 2014

2048 - Scoring (Part 2)

In Part 1 we talk about how to estimate the score of your game based on a single snapshot. However, we hadn't yet corrected for the fact that $4$'s sometimes spawn. So, to compensate for the number of $4$'s that spawn, we need to know how often they spawn! The online, open-source version has a $10\%$ chance of spawning a $4$, but based on nearly $38,000$ (No, I didn't count them all, I'll show you how I came about this in a little bit.) spawns on my iPhone app version, there is about a $15.3\%$ chance that a $4$ will spawn (leading me to believe that it's supposed to be a $15\%$ chance).

With that in mind, we need to have an estimate for the total number of tiles spawned. And under our old assumption (that only $2$'s spawn), we can compute that very easily. Consider the following. Every $2$ tile that spawns counts as a spawn. Therefore, every $4$ tile that we have is really two total tiles that spawned (the two $2$ tiles). The $8$'s are 4 spawns (two $4$ tiles each with two $2$ tiles spawning). It doesn't take long to figure out that each tiles has $$c_a = a/2$$ where $a$ is the face value of the tile. Thus we just sum this for each of the $16$ tiles on the board and we have a good estimate for the number of total tiles that spawned, $C_e$ $$C_e = \sum_{16\;Tiles}c_a$$ Well, we just assumed that all the spawns were number $2$ tiles. To compensate for the fact that $4$ tiles can spawn at a probability of $p_4$ we know the following two relationships: $$p_4 = \frac{C_4}{C_a}$$ $$C_a = C_e - C_4$$ where $C_4, C_a, $ and $C_e$ are the number of $4$ tiles spawned, the actual number of total tiles spawned, and the estimated total tiles spawned, respectively. The first relationship is fairly straight forward; it's how the probability of a four-spawn is defined. However, the second relationship involves a little more thought. We guessed, based on only $2$ tiles spawning that there are $C_e$ total spawns. If we actually know that there are $C_4$ four-spawns, then we have to account for the number of two-spawns that would've smashed into these $4$'s. So, since there are two $2$'s that make a $4$ we have $C_2 = C_e - 2C_4$ two-spawns. To get the actual number of spawns we add the two-spawns to the four-spawns $$C_a = C_2 + C_4 = (C_e - 2C_4) + C_4 = C_e - C_4$$ Plugging the second equation into the first and solving for $C_4$ we find out that $$C_4 = \frac{p_4C_e}{1+p_4}$$ Lastly, since each of these $4$'s that spawned does not give us 4 points (we assumed we got the points for all $4$ tiles), our expected score ($S_x$) is the estimate score ($S_e$) minus four times the number of $4$'s that spawned: $$S_x = S_e - 4C_4 = S_e - 4\frac{p_4C_e}{1+p_4}$$ Since we know $p_4$ and we know how to find $S_e$ (See Part 1) and $C_e$, we can compute our expected score. I played approximately 30 games with this and the average relative error for these guesses is about $0.2\%$. The results were even better once you scored above $10,000$. Only averaging the games with scores higher than $10,000$ (which was all but four of them), the average relative error was about $0.1\%$. Pretty cool results!

I said I would tell you how I came about the probability of a four-spawn of $15.3\%$. So I played a bunch of games and compared the actual score of the game to what I estimated it would be if there were only $2$ tiles that spawned. The difference between these two divided by four is the actual number of $4$ tiles that spawned. With that I used the two equations above and calculated what $p_4$ was!

There are a few more questions about 2048 that I still have. What is the highest score one can get? What is the lowest score? These questions (and others if I think of more) will be discussed in Part 3.