|
| 1 | +--- |
| 2 | +tags: |
| 3 | + - Original |
| 4 | +--- |
| 5 | + |
| 6 | +# Pell's Equation (Pell-Fermat Equation) |
| 7 | + |
| 8 | +## Statement |
| 9 | +We are given a natural number $d$. We need to find the smallest positive integer $x$ such that $x^{2} - d \cdot y^{2} = 1$ for some integer $y$. |
| 10 | + |
| 11 | +Alternative formulation: We want to find all the possible solutions of the equation $x^{2} - d \cdot y^{2} = 1$. |
| 12 | + |
| 13 | +## Solution |
| 14 | +Here we will consider the case when $d$ is not a perfect square and $d>1$. The case when $d$ is a perfect square is trivial. |
| 15 | +We can even assume that $d$ is square-free (i.e. it is not divisible by the square of any prime number) as we can absorb the factors of $d$ into $y$. |
| 16 | + |
| 17 | +$x^{2} - d \cdot y^{2} = ( x + \sqrt{d} \cdot y ) ( x - \sqrt{d} \cdot y ) = 1$ |
| 18 | + |
| 19 | +The first part $( x + \sqrt{d} \cdot y )$ is always greater than 1. And the second part $( x - \sqrt{d} \cdot y )$ is always less than 1. |
| 20 | + |
| 21 | +We will prove that all solutions to Pell's equation are given by powers of the smallest positive solution. Let's assume it to be |
| 22 | +$ x_{0} + y_{0} \cdot \sqrt{d} $ |
| 23 | + |
| 24 | +We use method of descent to prove it. suppose there is a solution $u + v \cdot \sqrt{d}$ such that $u^{2} - d \cdot v^{2} = 1 $ |
| 25 | +Therefore, $ ( x_{0} + \sqrt{d} \cdot y_{0} )^{n} < u + v \cdot \sqrt{d} < ( x_{0} + \sqrt{d} \cdot y_{0} )^{n+1} $ |
| 26 | + |
| 27 | +Multiplying the above inequality by $( x_{0} - \sqrt{d} \cdot y_{0} )^{n}$,(which is > 0 and < 1) we get |
| 28 | + |
| 29 | +$ 1 < (u + v \cdot \sqrt{d})( x_{0} - \sqrt{d} \cdot y_{0} )^{n} < ( x_{0} + \sqrt{d} \cdot y_{0} ) $ |
| 30 | +Because both $(u + v \cdot \sqrt{d})$ and $( x_{0} - \sqrt{d} \cdot y_{0} )^{n}$ have norm 1, their product is also a solution. |
| 31 | +But this contradicts our assumption that $( x_{0} + \sqrt{d} \cdot y_{0} )$ is the smallest solution. Therefore, there is no solution between $( x_{0} + \sqrt{d} \cdot y_{0} )^{n}$ and $( x_{0} + \sqrt{d} \cdot y_{0} )^{n+1}$. |
| 32 | + |
| 33 | +Hence, we conclude that all solutions are given by $( x_{0} + \sqrt{d} \cdot y_{0} )^{n}$ for some integer $n$. |
| 34 | + |
| 35 | +## To find the smallest positive solution |
| 36 | +### Expressing the solution in terms of continued fractions |
| 37 | +We can express the solution in terms of continued fractions. The continued fraction of $\sqrt{d}$ is periodic. Let's assume the continued fraction of $\sqrt{d}$ is $[a_{0}; \overline{a_{1}, a_{2}, \ldots, a_{r}}]$. The smallest positive solution is given by the convergent $[a_{0}; a_{1}, a_{2}, \ldots, a_{r}]$ where $r$ is the period of the continued fraction. |
| 38 | + |
| 39 | +The convergents $p_{n}/q_{n}$ are the rational approximations to $\sqrt{d}$ obtained by truncating the continued fraction expansion at each stage. These convergents can be computed recursively. |
| 40 | + |
| 41 | +Check whether the convergent satisfies the Pell's equation. If it does, then the convergent is the smallest positive solution. |
| 42 | + |
| 43 | +Let's take an example to understand this by solving the equation $x^{2} - 2 \cdot y^{2} = 1$. |
| 44 | +$\sqrt{2} = [1; \overline{2}] = 1 + 1/(2 + 1/(2 + 1/(2+ ...))) $. The convergents are $1/1, 3/2, 7/5, 17/12, 41/29, 99/70, \ldots$. |
| 45 | +Now check for each convergent whether it satisfies the Pell's equation. The smallest positive solution is $3/2$. |
| 46 | + |
| 47 | +### How to calculate the continued fraction of $\sqrt{d}$? |
| 48 | +Let's find the continued fraction of $\def\sf{\sqrt 7}\sf$. |
| 49 | + |
| 50 | +$\sf \approx 2.6457 = 2 + 0.6457$ |
| 51 | + |
| 52 | +$a_{0} = 2 $ |
| 53 | + |
| 54 | +Subtract $a_{0}$ from the number and take the reciprocal of the remaining number. |
| 55 | + |
| 56 | +That is, we calculate ${1\over \sf - 2} \approx 1.5486$. The integer part $a_{1}$ is $1$. |
| 57 | +So: |
| 58 | + $$\sf=2+\cfrac{1}{1+\cfrac1{\vdots}}$$ Where we haven't calculated the $\vdots$ part yet. |
| 59 | +To get that, we subtract $a_{1}$ from the number and take the reciprocal of the remaining number. That is, we calculate ${1\over 1.5486 - 1} \approx 1.8228$. The integer part $a_{2}$ is $1$. |
| 60 | +So: |
| 61 | + $$\sf=2+\cfrac{1}{1+\cfrac1{1+\cfrac1{\vdots}}}$$ |
| 62 | +Now ${1\over 1.8228 - 1} \approx 1.2153$. So $a_{3} = 1$. |
| 63 | +Continuing this process, ${1\over 1.2153 - 1} \approx 4.645$. So $a_{4} = 4$. |
| 64 | + |
| 65 | +$$\sf=2+\cfrac{1}{1+\cfrac1{1+\cfrac1{1+\cfrac4{\vdots}}}}$$ |
| 66 | + |
| 67 | +we get the continued fraction of $\sf$ as $[2; 1, 1, 4, 1, 1, 4, \ldots]$. |
| 68 | + |
| 69 | +It can also be calculated using only [integer based calculation](https://cp-algorithms.com/algebra/continued-fractions.html) |
| 70 | + |
| 71 | +### Finding the solution using Chakravala method |
| 72 | +The Chakravala method is an ancient Indian algorithm to solve Pell's equation. It is based on the Brahmagupta's identity of quadratic decomposition |
| 73 | +$(x_{1}^{2} - n \cdot y_{1}^{2}) \cdot (x_{2}^{2} - n \cdot y_{2}^{2}) = (x_{1} \cdot x_{2} + n \cdot y_{1} \cdot y_{2})^{2} - n \cdot (x_{1} \cdot y_{2} + x_{2} \cdot y_{1})^{2}$ |
| 74 | +$(x_{1}^{2} - n \cdot y_{1}^{2}) \cdot (x_{2}^{2} - n \cdot y_{2}^{2}) = (x_{1} \cdot x_{2} - n \cdot y_{1} \cdot y_{2})^{2} - n \cdot (x_{1} \cdot y_{2} - x_{2} \cdot y_{1})^{2}$ |
| 75 | + |
| 76 | +And Bhaskara's Lemma: |
| 77 | + If $x^{2} - n \cdot y^{2} = k$, then $ ( \frac{ m \cdot x + n \cdot y }{k})^{2} - n \cdot ( \frac{ x + m \cdot y }{k})^{2} = \frac{m^2 - n}{k}$ |
| 78 | + |
| 79 | +Using above Brahmagupta's identity, If $(x_{1}, y_{1}, k_{1})$ and $(x_{2}, y_{2}, k_{2})$ satisfy $(x_{1}^{2} - y_1^{2}) \cdot (x_{2}^{2} - y_2^{2}) = k_{1} \cdot k_{2} $, then $(x_{1} \cdot x_{2} + n \cdot y_{1} \cdot y_{2}, x_{1} \cdot y_{2} + y_{1} \cdot x_{2}, k_{1} \cdot k_{2})$ is also a solution of $(x_{1} \cdot x_{2} + n \cdot y_{1} \cdot y_{2})^{2} - n \cdot (x_{1} \cdot y_{2} + x_{2} \cdot y_{1})^{2} = k_{1} \cdot k_{2}$ |
| 80 | + |
| 81 | +#### Steps |
| 82 | +1. Initialization:Choose an initial solution $(p_{0}, q_{0}, m_{0})$ where $p_{0}$ and $q_{0}$ are co-prime such that $p_{0}^{2} - N \cdot q_{0}^{2} = m_{0}$. Typically, start with $p_{0} = \lfloor \sqrt N \rfloor $, $q_{0} = 1$, $m_{0} = p_0^2 - N $. |
| 83 | +2. Key step: Find $x_{1}$ such that: $q_{0} \cdot x_{1} \equiv -p_{0} \pmod {\lvert m_{0}\rvert}$ and $\lvert x_{1}^2 - N \rvert$ is minimized. |
| 84 | + Update the triple $(p_{1}, q_{1}, m_{1}) = ( \frac{x_{1} \cdot p_{0} + N \cdot q_{0}}{\lvert m_{0} \rvert}, \frac{p_{0} + x_{1} \cdot q_{0}}{\lvert m_{0} \rvert}, \frac{x_1^{2} - N}{m_{0}})$. |
| 85 | +3. Termination: When $m_{k}=1$, the values of $p_{k}$ and $q_{k}$ are the smallest positive solution of the Pell's equation. |
| 86 | + |
| 87 | +##### Example |
| 88 | +Let's solve the equation $x^{2} - 13 \cdot y^{2} = 1$ using Chakravala method. |
| 89 | +1. Start with $(p_{0}, q_{0}, m_{0}) = (3, 1, -4)$ because $3^2 - 13 \cdot1^2 = -4$. |
| 90 | + |
| 91 | +2. Find $x_{1}$ such that $x_{1} \equiv -3 \pmod {4}$ and $\lvert x_{1}^2 - 13 \rvert$ is minimized. |
| 92 | +We get $x_{1} = 1$. Update the triple $(p_{1}, q_{1}, m_{1}) = ( \frac{1 \cdot 3 + 13 \cdot 1}{4}, \frac{3 + 1 \cdot 1}{4}, \frac{1^{2} - 13}{-4}) = (4, 1, 3)$. |
| 93 | +3. Substituting $(p_{1}, q_{1}, k_{1}) = (4, 1, 3)$ in key step, we get $x_{2} \equiv -4 \pmod 3$ and minimize $\lvert x_{2}^2 - 13 \rvert$ i.e, $x_{2} = 2$. Update the triple $(p_{2}, q_{2}, m_{2}) = ( \frac{2 \cdot 4 + 13 \cdot 1}{3}, \frac{4 + 2 \cdot 1}{3}, \frac{2^{2} - 13}{-3}) = (7, 2, -3)$. |
| 94 | +4. Substituting $(p_{2}, q_{2}, m_{2}) = (7, 2, -3)$ in key step, we get $2 \cdot x_{3} \equiv -7 \pmod 3$ and minimize $\lvert x_{3}^2 - 13 \rvert$ i.e, $x_{3} = 4$. Update the triple $(p_{3}, q_{3}, m_{3}) = ( \frac{4 \cdot 7 + 13 \cdot 2}{3}, \frac{7 + 4 \cdot 2}{3}, \frac{4^{2} - 13}{-3}) = (18, 5, -1)$. |
| 95 | +5. Substituting $(p_{3}, q_{3}, m_{3}) = (18, 5, -1)$ in key step, we get $5 \cdot x_{4} \equiv -18 \pmod 1$ and minimize $\lvert x_{4}^2 - 13 \rvert$ i.e, $x_{4} = 4$. Update the triple $(p_{4}, q_{4}, m_{4}) = ( \frac{4 \cdot 18 + 13 \cdot 5}{1}, \frac{18 + 4 \cdot 5}{1}, \frac{4^{2} - 13}{-1}) = (137, 38, -3)$. |
| 96 | +6. Substituting $(p_{4}, q_{4}, m_{4}) = (137, 38, -3)$ in key step, we get $ 38 \cdot x_{5} \equiv -137 \pmod 3$ and minimize $\lvert x_{5}^2 - 13 \rvert$ i.e, $x_{5} = 2$. Update the triple $(p_{5}, q_{5}, m_{5}) = ( \frac{2 \cdot 137 + 13 \cdot 38}{3}, \frac{137 + 2 \cdot 38}{3}, \frac{2^{2} - 13}{-3}) = (256, 71, 3)$. |
| 97 | +7. Substituting $(p_{5}, q_{5}, m_{5}) = (256, 71, 3)$ in key step, we get $ 71 \cdot x_{6} \equiv -256 \pmod 3$ and minimize $\lvert x_{6}^2 - 13 \rvert$ i.e, $x_{6} = 4$. Update the triple $(p_{6}, q_{6}, m_{6}) = ( \frac{4 \cdot 256 + 13 \cdot 71}{3}, \frac{256 + 4 \cdot 71}{3}, \frac{4^{2} - 13}{3}) = (649, 180, 1)$. |
| 98 | + |
| 99 | +## Implementation |
| 100 | +```cpp |
| 101 | +bool isSquare(long long n) { |
| 102 | + long long sqrtN = (long long)sqrt(n); |
| 103 | + return sqrtN * sqrtN == n; |
| 104 | +} |
| 105 | + |
| 106 | +long long mod(long long a, long long b) { |
| 107 | + return (a % b + b) % b; |
| 108 | +} |
| 109 | + |
| 110 | +long long modInv(long long a, long long b) { |
| 111 | + long long b0 = b, x0 = 0, x1 = 1; |
| 112 | + if (b == 1) return 1; |
| 113 | + while (a > 1) { |
| 114 | + long long q = a / b; |
| 115 | + long long temp = b; |
| 116 | + b = a % b; |
| 117 | + a = temp; |
| 118 | + temp = x0; |
| 119 | + x0 = x1 - q * x0; |
| 120 | + x1 = temp; |
| 121 | + } |
| 122 | + if (x1 < 0) x1 += b0; |
| 123 | + return x1; |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +// Chakravala method for solving Pell's equation |
| 128 | +pair<long long, long long> chakravala(int n) { |
| 129 | + // Check if n is a perfect square |
| 130 | + if (isSquare(n)) { |
| 131 | + throw invalid_argument("n is a perfect square. No solutions exist for Pell's equation."); |
| 132 | + } |
| 133 | + |
| 134 | + // Initial values |
| 135 | + double sqrt_n = sqrt(n); |
| 136 | + long long a = (long long)floor(sqrt_n); // Initial a |
| 137 | + long long b = 1; // Initial b |
| 138 | + long long k = a * a - n; // Initial k |
| 139 | + |
| 140 | + int steps = 0; // Step counter for iterations |
| 141 | + |
| 142 | + // Repeat until k = 1 |
| 143 | + while (k != 1) { |
| 144 | + long long absK = abs(k); |
| 145 | + |
| 146 | + // Find m such that k | (a + bm), and minimize |m^2 - n| |
| 147 | + long long m; |
| 148 | + if (absK == 1) { |
| 149 | + m = (long long)round(sqrt(n)); // round to nearest integer |
| 150 | + } else { |
| 151 | + long long r = mod(-a, absK); // Negative of a mod(k) // (a + m*b)/|k| |
| 152 | + long long s = modInv(b, absK); // Modular inverse of b mod(k) |
| 153 | + m = mod(r * s, absK); // Compute m for (a + b*m) mod(k) = 0 |
| 154 | + |
| 155 | + // Approximate value of m |
| 156 | + // m = m + ((long long)floor((sqrt_n - m) / absK)) * absK; |
| 157 | + |
| 158 | + // Adjust m to ensure m < sqrt(n) < m + k |
| 159 | + while (m > sqrt(n)) m -= absK; |
| 160 | + while (m + absK < sqrt_n) m += absK; |
| 161 | + |
| 162 | + // Select closest value to n |
| 163 | + if (abs(m * m - n) > abs((m + absK) * (m + absK) - n)) { |
| 164 | + m = m + absK; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + // Print the current triple |
| 169 | + cout << "[a = " << a << ", b = " << b << ", k = " << k << "]" << endl; |
| 170 | + |
| 171 | + // Update a, b, k using the recurrence relations |
| 172 | + long long alpha = a; |
| 173 | + a = (m * a + n * b) / absK; |
| 174 | + b = (alpha + m * b) / absK; |
| 175 | + k = (m * m - n) / k; |
| 176 | + |
| 177 | + // Increment step counter |
| 178 | + steps++; |
| 179 | + } |
| 180 | + |
| 181 | + // Print final result |
| 182 | + cout << a << "^2 - " << n << " x " << b << "^2 = 1 in " << steps << " calculations." << endl; |
| 183 | + |
| 184 | + // Return the solution as a pair (a, b) |
| 185 | + return {a, b}; |
| 186 | +} |
| 187 | + |
| 188 | +``` |
| 189 | +
|
| 190 | +## References |
| 191 | +- [Pell's equation - Wikipedia](https://en.wikipedia.org/wiki/Pell%27s_equation) |
| 192 | +- [Periodic Continued Fractions](https://en.wikipedia.org/wiki/Periodic_continued_fraction) |
| 193 | +- [Chakravala Method](http://publications.azimpremjifoundation.org/1630/1/3_The%20Chakravala%20Method.pdf) |
| 194 | +- [Pythagorean triples and Pell's equations - Codeforces](https://codeforces.com/blog/entry/116313) |
| 195 | +
|
| 196 | +## Problems |
| 197 | +- [Project Euler 66](https://projecteuler.net/problem=66) |
| 198 | +- [Hackerrank ProjectEuler-066](https://www.hackerrank.com/contests/projecteuler/challenges/euler066/problem) |
0 commit comments