Diophantine Equation
Consider quadratic Diophantine equations of the form: x^2 - Dy^2 = 1 For example, when D = 13, the minimal solution in x is 649^2 - 13 x 180^2 = 1. It can be assumed that there are no solutions in...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Consider quadratic Diophantine equations of the form: \[x^2 - Dy^2 = 1\] For example, when \(D=13\), the minimal solution in \(x\) is \(649^2 - 13 \times 180^2 = 1\).
It can be assumed that there are no solutions in positive integers when \(D\) is square.
By finding minimal solutions in \(x\) for \(D = \{2, 3, 5, 6, 7\}\), we obtain the following: \begin {align*} 3^2 - 2 \times 2^2 &= 1\\ 2^2 - 3 \times 1^2 &= 1\\ {\color {red}{\mathbf 9}}^2 - 5 \times 4^2 &= 1\\ 5^2 - 6 \times 2^2 &= 1\\ 8^2 - 7 \times 3^2 &= 1 \end {align*}
Hence, by considering minimal solutions in \(x\) for \(D \le 7\), the largest \(x\) is obtained when \(D=5\).
Find the value of \(D \le 1000\) in minimal solutions of \(x\) for which the largest value of \(x\) is obtained.
Problem 66: Diophantine Equation
Mathematical Analysis
Pell’s Equation
The equation is known as Pell’s equation. For non-square , this equation always has infinitely many solutions in positive integers.
Connection to Continued Fractions
The fundamental (minimal) solution of Pell’s equation is obtained from the convergents of the continued fraction expansion of .
Theorem: The continued fraction expansion of is periodic:
where is the period length.
Theorem: Let denote the -th convergent of the continued fraction of . Then the fundamental solution is:
- If is even:
- If is odd:
Convergent Computation
The convergents satisfy the recurrence:
with and .
Continued Fraction Algorithm for
Initialize: , , .
Recurrence:
The period ends when .
Proof of Correctness
Claim: The fundamental solution of is given by where is the period (or if is odd).
Proof sketch:
- By Lagrange’s theorem, has a periodic continued fraction.
- For any convergent , we have .
- If , then .
- The theory of continued fractions guarantees that the first such solution occurs at the end of the first period (or second period if the period is odd).
Editorial
We scan all non-square values of up to . For each one, we compute the period of the continued fraction of and use its parity to determine which convergent yields the fundamental solution of Pell’s equation. We then generate convergents up to that index, extract the minimal numerator , and keep the value of for which this minimal is largest.
Pseudocode
Initialize the best pair as
current best D = 0
current best minimal x = 0
For each integer D from 2 through 1000:
if D is a perfect square, move on to the next value
compute the period length of the continued fraction of sqrt(D)
from that parity, determine which convergent carries the fundamental solution
regenerate the continued fraction up to the required convergent
read off its numerator x
if x is larger than the current best minimal x:
replace the recorded best pair by (D, x)
Return the recorded value of D.
Complexity
- Time: where and is the maximum period length of any continued fraction for .
- The period length is in the worst case.
- Space: per (only need to track current and previous convergents).
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
// Big integer class (base 10^9) for Pell's equation
// Needed since minimal x can be ~10^37
struct BigInt {
vector<int> d; // digits in base 10^9, least significant first
BigInt() {}
BigInt(long long v) {
if (v == 0) d.push_back(0);
while (v > 0) { d.push_back(v % 1000000000); v /= 1000000000; }
}
bool operator>(const BigInt& o) const {
if (d.size() != o.d.size()) return d.size() > o.d.size();
for (int i = (int)d.size()-1; i >= 0; i--)
if (d[i] != o.d[i]) return d[i] > o.d[i];
return false;
}
BigInt operator+(const BigInt& o) const {
BigInt res;
int carry = 0;
for (int i = 0; i < (int)max(d.size(), o.d.size()) || carry; i++) {
long long s = carry;
if (i < (int)d.size()) s += d[i];
if (i < (int)o.d.size()) s += o.d[i];
res.d.push_back(s % 1000000000);
carry = s / 1000000000;
}
return res;
}
BigInt operator*(long long v) const {
BigInt res;
long long carry = 0;
for (int i = 0; i < (int)d.size() || carry; i++) {
long long cur = carry;
if (i < (int)d.size()) cur += (long long)d[i] * v;
res.d.push_back(cur % 1000000000);
carry = cur / 1000000000;
}
while (res.d.size() > 1 && res.d.back() == 0) res.d.pop_back();
return res;
}
};
// Solve Pell's equation x^2 - D*y^2 = 1 using continued fractions
// Returns the fundamental (minimal positive) solution x
BigInt solve_pell(int D) {
int a0 = (int)sqrt((double)D);
if (a0 * a0 == D) return BigInt(0); // perfect square
// Find period length of continued fraction of sqrt(D)
int m = 0, d = 1, a = a0;
int period = 0;
do {
m = d * a - m;
d = (D - m * m) / d;
a = (a0 + m) / d;
period++;
} while (a != 2 * a0);
// If period is even, solution is convergent p_{period-1}
// If period is odd, solution is convergent p_{2*period-1}
int target = (period % 2 == 0) ? period - 1 : 2 * period - 1;
// Compute convergent p_{target}
m = 0; d = 1; a = a0;
BigInt pm2(1), pm1(a0); // p_{-1}=1, p_0=a0
if (target == 0) return pm1;
for (int i = 1; i <= target; i++) {
m = d * a - m;
d = (D - m * m) / d;
a = (a0 + m) / d;
BigInt pn = pm1 * a + pm2;
pm2 = pm1;
pm1 = pn;
}
return pm1;
}
int main() {
ios_base::sync_with_stdio(false);
BigInt max_x(0);
int best_D = 0;
for (int D = 2; D <= 1000; D++) {
int s = (int)sqrt((double)D);
if (s * s == D) continue;
BigInt x = solve_pell(D);
if (x > max_x) {
max_x = x;
best_D = D;
}
}
cout << best_D << endl;
return 0;
}
"""
Project Euler Problem 66: Diophantine Equation (Pell's Equation)
Find the value of D <= 1000 for which the minimal solution x of
x^2 - D*y^2 = 1 is maximized.
Uses continued fraction expansion of sqrt(D) to find fundamental solutions.
"""
from math import isqrt
def solve_pell(D):
"""
Solve x^2 - D*y^2 = 1 (Pell's equation) for the fundamental solution.
Returns the minimal positive x using continued fraction expansion of sqrt(D).
"""
a0 = isqrt(D)
if a0 * a0 == D:
return None # D is a perfect square, no solution
# Find the period length of the continued fraction of sqrt(D)
m, d, a = 0, 1, a0
period = 0
while True:
m = d * a - m
d = (D - m * m) // d
a = (a0 + m) // d
period += 1
if a == 2 * a0:
break
# If period is even, solution is at convergent index (period - 1)
# If period is odd, solution is at convergent index (2*period - 1)
target = period - 1 if period % 2 == 0 else 2 * period - 1
# Compute convergent p_{target}
m, d, a = 0, 1, a0
p_prev, p_curr = 1, a0 # p_{-1} = 1, p_0 = a0
if target == 0:
return p_curr
for i in range(1, target + 1):
m = d * a - m
d = (D - m * m) // d
a = (a0 + m) // d
p_prev, p_curr = p_curr, a * p_curr + p_prev
return p_curr
def main():
max_x = 0
best_D = 0
for D in range(2, 1001):
x = solve_pell(D)
if x is not None and x > max_x:
max_x = x
best_D = D
print(best_D)
if __name__ == "__main__":
main()