The guide
The smallest answer to a one-line equation can run to hundreds of digits
Pell's equation asks for whole numbers whose squares sit exactly one apart after a scaling, and the continued fraction of a square root finds the smallest pair without guessing.
- 649 and 180 the smallest positive solution for n = 13
- 18 and 5 n = 13 after one period: solves the negative form
- 1766319049 x for n = 61, found by Bhaskara II
- 206,545 digits in x for the cattle problem
- 9949 the last record value of n below 10,000
Read the full guide Fold the guide
Try to find whole numbers x and y so that x squared is exactly one more than 13 times y squared. With y equal to 1 the target is 14, which is not a square. With y equal to 2 it is 53, and with y equal to 3 it is 118. You can keep going for a long while. The first pair that works is x = 649 and y = 180, because 649 squared is 421,201 and 13 times 180 squared is 421,200. Nothing in the question hints that the answer lies so far out.
What the equation asks
Pell’s equation is x squared minus n times y squared equals 1, where n is a positive whole number that is not a perfect square and both unknowns are whole numbers. Drawn on a graph it is a hyperbola, and the solutions are the places where the curve passes through a point with whole-number coordinates. The pair x = 1, y = 0 always works and is called trivial. Joseph Louis Lagrange proved that when n is not a perfect square there are infinitely many other solutions.
The restriction on n is not decoration. If n were s squared, the left side would factor as (x minus s y) times (x plus s y), and two whole numbers multiply to 1 only when both are 1 or both are minus 1. That forces y to be 0, and only the trivial solution survives.
The history is older than the name. Brahmagupta found a solution of an equation of this kind in his Brahmasphutasiddhanta around the year 628, and in the twelfth century Bhaskara II is generally credited with the chakravala method, a cyclic procedure that always ends in a solution. Bhaskara used it on the notorious case n = 61 and got x = 1,766,319,049 and y = 226,153,980.
Europe came to the problem later. William Brouncker was the first European to solve the equation, and Pierre de Fermat posed it as a challenge in a 1657 letter. Bernard Frenicle de Bessy reported that Fermat had found the smallest solution for every n up to 150 and asked John Wallis for the cases n = 151 and n = 313. Wallis and Brouncker both answered.
The name is a historical accident. Leonhard Euler credited Brouncker’s solution to John Pell, who in fact had little to do with the equation, although it appears in a book by Johann Rahn that Pell helped to bring into English. Lagrange worked out the general theory between 1766 and 1769, and it rests on continued fractions.
Reading the answer off a continued fraction
Any positive number can be written as a whole part plus a fraction whose denominator is again a whole part plus a fraction, and so on. For a rational number the chain ends. For the square root of a non-square whole number it never ends, but it repeats: after the whole part comes a block of numbers that cycles forever, and the last number in the block is always twice the whole part. The numbers in the block before that last one read the same forwards and backwards.
The square root of 7 is 2 followed by the repeating block 1, 1, 1, 4. The square root of 13 is 3 followed by the block 1, 1, 1, 1, 6. The length of the block is the period, four for 7 and five for 13. Cut the chain short and you get a convergent, a fraction h over k that approximates the root. Each pair comes from the two before it: the new h is the latest term times the previous h plus the h before that, and k is built the same way. For the square root of 13 the convergents are 3/1, 4/1, 7/2, 11/3, 18/5, 119/33, 137/38, 256/71, 393/109 and 649/180.
There is a reason a Pell solution hides among them. If x squared minus n y squared equals 1, then x squared over y squared equals n plus 1 over y squared, so x over y is extremely close to the square root of n. The convergents are the best fractions of that kind, and the smallest solution is always one of them.
The rule for choosing which one is short. When the period is even, take the convergent just before the end of the first period. When the period is odd, take the one just before the end of the second period.
Take n = 7. The period has four terms, so the answer sits at the end of the first pass: the convergents are 2/1, 3/1, 5/2 and 8/3, and 8 squared minus 7 times 3 squared is 64 minus 63, which is 1. Take n = 13. The period has five terms, so the first pass ends at 18/5, where 18 squared minus 13 times 5 squared is 324 minus 325, which is minus 1. That misses, and in the wrong direction. A second pass through the period reaches 649/180, which scores plus 1.
Run down the column of h squared minus 13 k squared and it reads minus 4, 3, minus 3, 4, minus 1, 4, minus 3, 3, minus 4, 1. Only the ends of periods land on 1 or minus 1. A method that stops after one period for every n would report 18 and 5 for n = 13. Those numbers are not nonsense, but they answer a different question.
One solution makes all of them, and minus one is fussy
Once the smallest pair x1, y1 is known, every other positive solution is a power of x1 plus y1 times the square root of n. Multiply out the powers, match the whole parts and the root parts, and you get a recurrence: the next x is x1 times the current x plus n times y1 times the current y, and the next y is x1 times the current y plus y1 times the current x. For n = 13 the solution after 649 and 180 is 842,401 and 233,640.
Algebraic number theory says the same thing in other words. A pair solves Pell’s equation exactly when x plus y times the square root of n is a unit of norm 1 in the numbers built from the whole numbers and that root, and Dirichlet’s unit theorem says all such units are powers of a single fundamental one.
The negative equation, x squared minus n y squared equal to minus 1, yields to the same method. It has a solution exactly when the period is odd. That is why 13 works, with 18 and 5, and why 7 never will, since its period has four terms.
A quick test can rule some values out but cannot confirm any. A solution requires that n is not divisible by 4 and has no prime factor of the form 4k plus 3. So x squared minus 3 y squared equals minus 1 is never solvable, while x squared minus 5 y squared equals minus 1 can be, with x = 2 and y = 1. The condition is necessary only: 34 is 2 times 17 and passes it, yet the square root of 34 has the period 1, 4, 1, 10 of even length, so no solution exists. The values of n that do work begin 2, 5, 10, 13, 17, 26, 29, 37, 41 and 50.
When the negative equation does have a solution u, v, squaring u plus v times the root gives the positive fundamental solution. For 13, 18 plus 5 root 13 squared is 649 plus 180 root 13, which you can confirm by hand. Negative solutions are the odd powers; positive ones are the even powers.
Numbers that outgrow the page
The size of the smallest solution is wildly irregular. For n = 61 it is ten digits. For n = 109 it is 158,070,671,986,249 and 15,140,424,455,100. For n = 313, the case Frenicle set as a challenge, x is 32,188,120,829,134,849.
The smallest x for each n, which runs 3, 2, 1, 9, 5, 8, 3, 1, 19 from n = 2 to n = 10 with 1 standing in for the squares, is catalogued by the Online Encyclopedia of Integer Sequences. The values of n whose solution is larger than for every smaller n form a sparse list that begins 2, 5, 10, 13, 29, 46, 53, 61, 109 and 181. Thirty-three of them lie below 10,000, and the last is 9949.
Archimedes’s cattle problem shows how far this goes. Reformulated as a Pell equation it has n = 410286423278424, and its fundamental solution has 206,545 digits when written out. Carl Amthor found the general solution in 1880 using logarithm tables, and the full digits were first printed by computer at the University of Waterloo in 1965.
Computing a huge answer is not the trouble, since the continued fraction method takes time close to the size of the solution. The trouble is that the solution can have far more digits than any polynomial in the number of digits of n. Long answers can sometimes be written compactly as a product of much smaller numbers, which is how the cattle solution is expressed with pieces of 45 and 41 digits.
Running the exact method over every non-square n up to 20,000 shows the longest solution has 278 digits, at n = 17,341. Up to 100,000 the longest has 724 digits, at n = 92,821. Those are measured by running the method and checking each result by substitution, not taken from a table.
Ordinary floating-point numbers hold whole numbers exactly only up to 9,007,199,254,740,991, so these answers need integers of any size. JavaScript’s BigInt type provides them, with the caveat that its values cannot be mixed with ordinary numbers in the same calculation, and the scan over many values of n runs in a Web Worker so that the page stays responsive.
This page solves only the equation with 1 or minus 1 on the right. It does not search for solutions when the right side is some other number, it does not treat other quadratic equations in two unknowns, and it proves nothing: it computes the smallest pair, builds the rest from the recurrence and checks every one.
Sources
Every source consulted for this guide. Links checked November 10, 2026.
- Pell's equation Wikipedia
- Periodic continued fraction Wikipedia
- Continued fraction Wikipedia
- Chakravala method Wikipedia
- Archimedes's cattle problem Wikipedia
- John Pell MacTutor History of Mathematics, University of St Andrews
- Take solution to Pellian equation x^2 - n*y^2 = 1 with smallest positive y (A002350) The On-Line Encyclopedia of Integer Sequences
- Numbers k such that Pell equation x^2 - k*y^2 = -1 is soluble (A031396) The On-Line Encyclopedia of Integer Sequences
- Value of D for incrementally largest values of minimal x satisfying Pell equation x^2-Dy^2=1 (A033316) The On-Line Encyclopedia of Integer Sequences
- BigInt MDN Web Docs
- Web Workers API MDN Web Docs