In the popular Pokémon franchise, there is a numbering of all the many species of Pokémon. For example, Bulbasaur has number 1, Pikachu has number 25, and Eevee has number 133.
Genesect is another one of the many kinds of Pokémon. It has number 649. And being a mysterious Pokémon, it poses this riddle for you.
The equation x 2 − 6 4 9 y 2 = 1 has infinitely many integral solutions (both x , y are integers). Among them, consider those where x , y are strictly positive. There is one solution where y is minimized. Find this value of y .
(This is a computer science problem. You are allowed to program your solution. But testing each possible value of y one by one will most likely not give you the solution any time soon; the answer has 14 digits.)
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Beautifully typed solution with recurrence relation!
I'm having trouble finding the proof that the solutions will always be given by the convergents of the continued fraction expansion. (After all, my reasoning that "those will make the answer close" should have also worked for any other N in x 2 − D y 2 = N , when x , y are big, but turns out for other N the solutions don't come from the expansion.) Can you give a link to that?
Log in to reply
The values N = ± 1 are special. The very slim (95 pages) book by Alan Baker, "A concise introduction to the theory of numbers" (basically the published notes of a lecture series) has all the details, but many other more substantial books (e.g. Davenport) do as well.
very nice solution, Mark. Wes
We will generalize the problem to x 2 − D y 2 = 1 , where D is a positive integer and not a square number. (If D is not positive, we have − 1 ≤ x ≤ 1 ; just test them. If D is a square number, we can factorize the left hand side into a product of two integers, and so the answer is again simple.)
If you know it, this is Pell's equation. There has been a lot of research about this; in fact, there is an algorithm where, given D , it can generate all the solutions efficiently. The idea is that if x 2 − D y 2 = 1 , then x 2 and D y 2 are very close to each other, and so y 2 x 2 ≈ D , or y x ≈ D . We can then use the continued fraction expansion of D to obtain the smallest solution, because we know approximations obtained from continued fractions are particularly accurate (e.g. 1 1 3 3 5 5 approximation to π is pretty accurate, because it's obtained from the continued fraction expansion).
For example, consider D = 1 1 . The continued fraction expansion is
1 1 = 3 + 3 + 6 + 3 + 6 + … 1 1 1 1
Taking the approximations, we have:
3 3 + 3 1 3 + 3 + 6 1 1 3 + 3 + 6 + 3 1 1 1 … = 1 3 = 3 1 0 = 1 9 6 3 = 6 0 1 9 9
Thus, the candidate solutions are those approximations: 1 3 , 3 1 0 , 1 9 6 3 , 6 0 1 9 9 , … . By checking one by one, we find out that ( 1 0 , 3 ) is a solution to x 2 − 1 1 y 2 = 1 . And later in the sequence, we find another solution ( 1 9 9 , 6 0 ) , and so on; continuing the sequence will give more solutions.
Moreover, computing these approximations is not too difficult, and it can be done way faster than brute-force search. First, we can generate the continued fraction expansion:
This will give a never-ending stream of the expansion. How can we use it to compute the approximation? Easy. Each time we obtain a term, we try cutting it off there and finding the current approximation:
Finally, it's trivial to check whether our candidate is indeed a solution or not. This might cause problems in languages that don't support arbitrary-precision integers, though. You can either move to a language that supports it, like Python or Java, or you can rig in some way to support larger integers. Fortunately, for this problem, it's possible to do with a small array in C++ to represent a big number; details follow.
Either way, we have found a solution. How can we guarantee that's the minimum? That's yet more theory. The intuitive reason is precisely the same reason why we're looking at continued fraction expansions: approximations obtained in any other way are weaker and so the difference between y 2 x 2 and D will become bigger, which means x 2 − D y 2 will also become bigger, far from 1.
Anyway, for D = 6 4 9 the smallest solution is ( 1 1 2 3 5 9 3 2 2 6 1 6 2 1 9 9 , 4 4 1 0 4 8 9 2 0 9 5 3 8 0 ) .
This is a way to use the fact that C++ supports 64-bit integers to make a data structure that is effectively 120-bit integers. Any such 120-bit integer will in fact be an array of four 64-bit integers
a[3..0]
, which will represent the number
2
9
0
a
[
3
]
+
2
6
0
a
[
2
]
+
2
3
0
a
[
1
]
+
2
0
a
[
0
]
. Whenever we do something to this array, we will immediately perform necessary carries so all
a[i]
remain 30-bit. (We don't use 32 bits because there's a subtlety later that we need to handle.)
Now, why will this be enough? Straightforward checking requires us to multiply
x
by itself, as well as 649 with
y
2
. Since we're told that
y
has 14 digits, this means
y
has at most 47 bits. Since
x
≈
y
6
4
9
,
x
has at most 53 bits. This fits inside 60 bits, so if we represent
x
in our array method, we will have
x[3], x[2]
zero. Likewise,
y[3], y[2]
will also be zero.
To compute
x
2
and store it into
x2
, we simply use schoolbook multiplication:
x2[2] = x[1] * x[1], x2[1] = x[1] * x[0] + x[0] * x[1], x2[0] = x[0] * x[0]
. (The reason we use 30 bits per element is clear here: if we use the full 32 bits,
x2[1]
can overflow.) This is schoolbook multiplication except each digit ranges up to
2
3
0
instead of up to 9. Remember that after we do this multiplication, we need to perform carries: if
x2[0]
is larger than
2
3
0
, we take the quotient when dividing by
2
3
0
and add it to
x2[1]
, so
x2[0]
can simply store the remainder.
We compute
6
4
9
y
2
similarly. One method that is easy to prove is to compute
6
4
9
y
times
y
; we can find out that
6
4
9
y
fits in 57 bits, so it's not a problem. We obtain
y2
that stores this result.
Finally, to check the computation, we can simply do schoolbook subtraction to compute
x2 - y2
. Just compute the difference element-by-element, then perform any necessary carries. If the result is 1 (that is, element 0 has value 1, and the rest have value 0), then we have found our answer.
Problem Loading...
Note Loading...
Set Loading...
The continued fraction expansion of 6 4 9 is [ a 0 , a 1 , a 2 , … ] = [ 2 5 , 2 , 9 , 1 , 2 , 3 , 1 , 1 , 2 , 1 , 4 , 1 , 1 6 , 6 , 3 , 4 , 3 , 6 , 1 6 , 1 , 4 , 1 , 2 , 1 , 1 , 3 , 2 , 1 , 9 , 2 , 5 0 ] In particular, we note that it is periodic of period 3 0 . Since 3 0 is even, it is standard continued fraction bookwork that positive integer solutions of the equation x 2 − 6 4 9 y 2 = 1 are given by x = p 3 0 u − 1 , y = q 3 0 u − 1 u ∈ N where q n p n = [ a 0 , a 1 , … , a n ] are the convergents of the continued fraction expansion. These are most readily determined by the recurrence relations p n q n = a n p n − 1 + p n − 2 = a n q n − 1 + q n − 2 p 0 = a 0 q 0 = 1 p − 1 = 1 q − 1 = 0 Since we calculate p 2 9 = 1 1 2 3 5 9 3 2 2 6 1 6 2 1 9 9 q 2 9 = 4 4 1 0 4 8 9 2 0 9 5 3 8 0 we have the answer.