Project Euler 100 - can't understand solution

736 Views Asked by At

Problem

I solved this using OEIS (finding smaller values and then searching OEIS for related sequences, it turned out that the exact sequence I needed was there) sequence.

Looking at the thread, the only solution that explained this was:

I did this one by hand, $b = \text{number of blue elements}$, $t = \text{total elements} $

$$ \begin{aligned} \dfrac{t^2 - t}{b^2 - b} &= 2\\ t^2 - 2b^2 &= t - 2b \end{aligned} $$

But if $t = pn$ and $b = pk$ and $p = 2b - t$:

$$ \begin{aligned} p^2n^2 - 2p^2b^2 = pt - 2pt = -p(2b - t) &= -p^2\\ n^2 - 2b^2 &= -1 \end{aligned} $$

That is a diophantine equation, and its easy to get some solutions, I took the first solution where $pn = (2b - t)t > 10^{12}$: $n = 1607521$, $k = 1136689$

Everything here makes sense, however, I don't I understand how he came up with the idea to express $t$ as $p n$ and $b$ as $p k$ (My guess is that $p$ is GCD of $b$ and $t$) And I definitely don't understand why he made $p = 2b - t$ (the only relation I can see to the equation is that it's the negative of the RHS, however I don't see how this is useful as a value of $p$, and seems quite arbitrary to me)

1

There are 1 best solutions below

0
On BEST ANSWER

There are very serious problems with the material you quote. The second equality in the first line of $$ \begin{aligned} p^2n^2 - 2p^2b^2 = pt - 2pt = -p(2b - t) &= -p^2\\ n^2 - 2b^2 &= -1 \end{aligned} $$ is surely wrong since it implies $b=t$, which forces $b=t=1$. Replacing $pt-2pt$ with $pt-2pb$ might make sense, but it would still be unclear where the first equality, $p^2n^2 - 2p^2b^2 = pt - 2pb$, comes from. Rather, it seems likely that the first equality is supposed to be $p^2n^2 - 2p^2k^2 = pt - 2pk$, since that follows immediately from $t^2-2b^2=t-2b$ by replacing $t$ with $pn$ and $b$ with $pk$. Assuming this, we get $$ p^2n^2 - 2p^2k^2 = pn - 2pk = -p(2k - n), $$ but it is no longer true that this equals $-p^2$ unless the condition $p=2k-n$ is imposed instead of $p=2b-t$. If we do this, we get $n^2-2k^2=-1$ instead of $n^2-2b^2=-1$. This actually may be what the author intended because the given solution, $n=1607521$, $k=1136689$, does indeed satisfy $n^2-2k^2=-1$. Furthermore, if $p=2k-n$ is used to calculate $p$ and $t=pn$, $b=pk$ are used to calculate $t$ and $b$, we do get the correct answer to the original question.

So it seems reasonable to revise the quoted passage as follows:

But if $t=pn$ and $b=pk$ and $p=2k−n$: $$ \begin{aligned} p^2n^2 - 2p^2k^2 = pn - 2pk = -p(2k - n) &= -p^2\\ n^2 - 2k^2 &= -1 \end{aligned} $$ That is a diophantine equation, and its easy to get some solutions, I took the first solution where $pn=(2k−n)n>10^{12}$: $n=1607521$, $k=1136689$

The question them is: What justifies the assumption $p=2k-n$? In fact, as will be shown below, $p$ takes one of two possible forms, $p=2k-n$ or $p=\frac{1}{2}(2k-n)$, depending on whether $n$ is odd or even. Both possibilities have to be considered since there is no a priori reason why the answer to the original question should be one for which $n$ is odd. It seems possible that the author of the quoted passage was simply lucky in assuming that $p=2k-n$. Incidentally, you are correct in your surmise that $p=\gcd(t,b)$.

Here's the proof of the claim that $p=2k-n$ or $p=\frac{1}{2}(2k-n)$. Let $(t,b)$ be a solution in positive integers to $2b^2-t^2=2b-t$. One can readily show that $b\le t<2b$. Compute $p=\gcd(t,b)$ and let $b=pk$ and $t=pn$ so that $\gcd(n,k)=1$. Then $(2k^2-n^2)p=2k-n$.

Now $2k^2-n^2$ divides $2k-n$ and so $2k^2-n^2=\pm\gcd(2k^2-n^2,2k-n)$. Because $2k-n$ and hence $2k^2-n^2$ is positive, the sign will be positive. We now compute the needed GCD: $$ \begin{aligned} \gcd(2k^2-n^2,2k-n)&=\gcd(2k-n,2k^2-n^2-k(2k-n))\\ &=\gcd(2k-n,n^2-nk)\\ &=\gcd(2k-n,n(n-k)). \end{aligned} $$ If $n$ is odd, then $\gcd(2k-n,n)=\gcd(2k,n)=1$ so that $$ \begin{aligned} \gcd(2k^2-n^2,2k-n)&=\gcd(2k-n,n-k)\\ &=\gcd(n-k,k)=\gcd(n,k)=1. \end{aligned} $$ We conclude that $2k^2-n^2=1$. This is a negative Pell equation. In this case $p=2k-n$.

If $n=2r$ is even, so that $\gcd(2k-n,n)=\gcd(2(k-r),2r)=2$, then $$ \begin{aligned} \gcd(2k^2-n^2,2k-n)&=\gcd(2(k-r),2r(2r-k))\\ &=2\gcd(k-r,2r-k)=2\gcd(k-r,r)=2\gcd(k,r)=2. \end{aligned} $$ We conclude that $2k^2-n^2=2$. This is the case that the author of the quoted passage appears to have missed. Solutions are obtained from solutions to a Pell equation, $k^2-2r^2=1$. In this case $p=\frac{1}{2}(2k-n)=k-r$.

There is another way to relate the original problem to a negative Pell equation, which is simpler, but doesn't provide the common factor of $t$ and $b$. Complete the square: $$ \begin{aligned} t^2-t&=2(b^2-b)\\ (t-1/2)^2+1/4&=2(b-1/2)^2\\ (2t-1)^2+1&=2(2b-1)^2. \end{aligned} $$ Now look for pairs of odd numbers, $(x,y)$, that are solutions to the negative Pell equation $x^2-2y^2=-1$, and then take $t=(x+1)/2$, $b=(y+1)/2$.