Given some positive integer \$n\$ that is not a square, find the fundamental solution \$(x,y)\$ of the associated Pell equation

$$x^2 - n\cdot y^2 = 1$$


  • The fundamental \$(x,y)\$ is a pair of integers \$x,y\$ satisfying the equation where \$x\$ is minimal, and positive. (There is always the trivial solution \$(x,y)=(1,0)\$ which is not counted.)
  • You can assume that \$n\$ is not a square.


 n           x    y
 1           -    -
 2           3    2
 3           2    1
 4           -    -
 5           9    4
 6           5    2
 7           8    3
 8           3    1
 9           -    -
10          19    6
11          10    3
12           7    2
13         649    180
14          15    4
15           4    1
16           -    -
17          33    8
18          17    4
19         170    39
20           9    2
21          55    12
22         197    42
23          24    5
24           5    1
25           -    -
26          51    10
27          26    5
28         127    24
29        9801    1820
30          11    2
31        1520    273
32          17    3
33          23    4
34          35    6
35           6    1
36           -    -
37          73    12
38          37    6
39          25    4
40          19    3
41        2049    320
42          13    2
43        3482    531
44         199    30
45         161    24
46       24335    3588
47          48    7
48           7    1
49           -    -
50          99    14
51          50    7
52         649    90
53       66249    9100
54         485    66
55          89    12
56          15    2
57         151    20
58       19603    2574
59         530    69
60          31    4
61  1766319049    226153980
62          63    8
63           8    1
64           -    -
65         129    16
66          65    8
67       48842    5967
68          33    4
69        7775    936
70         251    30
71        3480    413
72          17    2
73     2281249    267000
74        3699    430
75          26    3
76       57799    6630
77         351    40
78          53    6
79          80    9
80           9    1
81           -    -
82         163    18
83          82    9
84          55    6
85      285769    30996
86       10405    1122
87          28    3
88         197    21
89      500001    53000
90          19    2
91        1574    165
92        1151    120
93       12151    1260
94     2143295    221064
95          39    4
96          49    5
97    62809633    6377352
98          99    10
99          10    1

Relevant OEIS sequences: A002350 A002349 A033313 A033317

  • \$\begingroup\$ Surprised that there isn't any challenge with the Pell equation yet, since it's pretty well-known I thought. At least, I do remember using it sometimes with Project Euler challenges. \$\endgroup\$ – Kevin Cruijssen Apr 16 at 14:00
  • \$\begingroup\$ @Fatalize "You can assume that \$n\$ is not a square." Would probably be clearer if the test cases omitted those imho, though. \$\endgroup\$ – Kevin Cruijssen Apr 16 at 14:05
  • 2
    \$\begingroup\$ @KevinCruijssen I considered that, but I thought it would be more confusing to omit some of the ns. (btw I was also surprized but I had this challenge in the sandbox for about a year) \$\endgroup\$ – flawr Apr 16 at 15:13
  • \$\begingroup\$ Related: projecteuler.net/problem=66 \$\endgroup\$ – steenbergh Apr 18 at 14:17

26 Answers 26


Piet, 612 codels

Takes n from standard input. Outputs y then x, space-separated.

Codel size 1: Pell's equation program with codel size 1

Codel size 4, for easier viewing: Pell's equation program with codel size 1


Check out this NPiet trace, which shows the program calculating the solution for an input value of 99.

I'm not sure whether I'd ever heard of Pell's equation before this challenge, so I got all of the following from Wikipedia; specifically, these sections of three articles:

Basically, what we do is this:

  1. Get \$n\$ from standard input.
  2. Find \$\lfloor\sqrt n\rfloor\$ by incrementing a counter until its square exceeds \$n\$, then decrement it once. (This is the first loop you can see in the trace, at top left.)
  3. Set up some variables for calculating \$x\$ and \$y\$ from the continued fraction of \$\sqrt n\$.
  4. Check whether \$x\$ and \$y\$ fit Pell's equation yet. If they do, output the values (this is the downwards branch about 2/3 of the way across) and then exit (by running into the red block at far left).
  5. If not, iteratively update the variables and go back to step 4. (This is the wide loop out to the right, back across the bottom, and rejoining not quite halfway across.)

I frankly have no idea whether or not a brute-force approach would be shorter, and I'm not about to try it! Okay, so I tried it.


Piet, 184 codels

This is the brute-force alternative I said (in my other answer) that I didn't want to write. It takes over 2 minutes to compute the solution for n = 13. I really don't want to try it on n = 29... but it checks out for every n up to 20, so I'm confident that it's correct.

Like that other answer, this takes n from standard input and outputs y then x, space-separated.

Codel size 1: Pell's equation program (brute-force variant) with codel size 1

Codel size 4, for easier viewing: Pell's equation program (brute-force variant) with codel size 4


Here's the NPiet trace for an input value of 5.

This is the most brutal of brute force, iterating over both \$x\$ and \$y\$. Other solutions might iterate over \$x\$ and then calculate \$y=\sqrt{\frac{x^2-1}{n}}\$, but they're wimps.

Starting from \$x=2\$ and \$y=1\$, this checks whether \$x\$ and \$y\$ have solved the equation yet. If it has (the fork at the bottom near the right), it outputs the values and exits.

If not, it continues left, where \$y\$ is incremented and compared to \$x\$. (Then there's some direction-twiddling to follow the zig-zag path.)

This last comparison is where the path splits around the mid-left. If they're equal, \$x\$ is incremented and \$y\$ is set back to 1. And we go back to checking if it's a solution yet.

I still have some whitespace available, so maybe I'll see if I can incorporate that square-root calculation without enlarging the program.

  • 2
    \$\begingroup\$ Haha I agree about the wimps that use square roots :D \$\endgroup\$ – flawr Apr 17 at 7:20

Brachylog, 16 bytes


Try it online!


;1↔                Take the list [1, Input]
   ;Ċz             Zip it with a couple of two unknown variables: [[1,I],[Input,J]]
      ×ᵐ           Map multiply: [I, Input×J]
        -1         I - Input×J must be equal to 1
          ∧        (and)
           Ċ√ᵐ     We are looking for the square roots of these two unknown variables
              ℕᵐ   And they must be natural numbers
                   (implicit attempt to find values that match those constraints)

Pari/GP, 34 bytes

PARI/GP almost has a built-in for this: quadunit gives the fundamental unit of the quadratic field \$\mathbb{Q}(\sqrt{D})\$, where \$D\$ is the discriminant of the field. In other words, quadunit(4*n) solves the Pell's equation \$x^2 - n \cdot y^2 = \pm 1\$. So I have to take the square when its norm is \$-1\$.

I don't know what algorithm it uses, but it even works when \$n\$ is not square-free.

Answers are given in the form x + y*w, where w denotes \$\sqrt{n}\$.


Try it online!


Wolfram Language (Mathematica), 46 bytes


Try it online!

  • 1
    \$\begingroup\$ Is it assured that this always finds the fundamental solution? \$\endgroup\$ – Greg Martin Apr 17 at 3:54
  • \$\begingroup\$ @GregMartin yes, it is.This always finds the first (minimum) solution. In this case this always returns {1,0}. That is why we have to choose x>1 and get the second (fundamental) solution \$\endgroup\$ – J42161217 Apr 17 at 9:13
  • 1
    \$\begingroup\$ I would like that to be true, but nothing in the documentation seems to indicate that.... \$\endgroup\$ – Greg Martin Apr 17 at 9:15
  • \$\begingroup\$ @GregMartin I have used this function many times and already knew how it worked. My only concern was to skip the first solution and that cost me those 5 extra bytes. You can easily write a program and test it (just to confirm millions of results) \$\endgroup\$ – J42161217 Apr 17 at 9:21

05AB1E, 17 16 14 bytes

Saved a byte thanks to Kevin Cruijssen.
Outputs [y, x]


Try it online!


∞                 # from the infinite list of numbers [1 ...]
 .Δ        }      # find the first number that returns true under
   n              # square
    *             # multiply with input
     >            # increment
      t©          # sqrt (and save to register as potential x)
        1%        # modulus 1
          _       # logical negation
            ®‚    # pair result (y) with register (x)
  • \$\begingroup\$ And you beat me to it again.. had a 17 byter as well, but it didn't work because Ų is bugged with decimals.. >.< Anyway, you can remove both , and add a trailing (no, the comma's are not the same ;p) to save a byte. \$\endgroup\$ – Kevin Cruijssen Apr 16 at 14:04
  • \$\begingroup\$ @KevinCruijssen: Thanks! Yeah I also went for Ų first noticing that it didn't work as expected. \$\endgroup\$ – Emigna Apr 16 at 14:08

Java 8, 74 73 72 bytes

n->{int x=1;var y=.1;for(;y%1>0;)y=Math.sqrt(-x*~++x/n);return x+" "+y;}

-1 byte thanks to @Arnauld.
-1 byte thanks to @OlivierGrégoire.

Try it online.


n->{                 // Method with double parameter and string return-type
  int x=1;           //  Integer `x`, starting at 1
  var y=.1;          //  Double `y`, starting at 0.1
  for(;y%1>0;)       //  Loop as long as `y` contains decimal digits:
    y=               //   Set `y` to:
      Math.sqrt(     //    The square-root of:
        -x*          //     Negative `x`, multiplied by
           ~++x      //     `(-x-2)` (or `-(x+1)-1)` to be exact)
                     //     (because we increase `x` by 1 first with `++x`)
               /n);  //     Divided by the input
  return x+" "+y;}   //  After the loop, return `x` and `y` with space-delimiter as result
  • 1
    \$\begingroup\$ 72 bytes, by changing n to a double, and x to an int, playing on the fact that x*x-1 is equal to (-x-1)*(-x+1). \$\endgroup\$ – Olivier Grégoire Apr 16 at 16:02
  • \$\begingroup\$ Well, I'm actually playing on the fact that (x+1)*(x+1)-1 is equal to -x*-(x+2), to be entirely correct. \$\endgroup\$ – Olivier Grégoire Apr 16 at 16:15

R, 66 56 54 53 52 47 45 bytes

a full program


-1 -2 thanks to @Giuseppe

-7 thanks to @Giuseppe & @Robin Ryder -2 @JAD

  • 1
    \$\begingroup\$ use .5 instead of 0.5 \$\endgroup\$ – Giuseppe Apr 16 at 15:22
  • 5
    \$\begingroup\$ 46 bytes. Finding the smallest value of x is equivalent to finding the smallest value of y. This allows you to save 2 bytes because expressing x in terms of y is shorter than the other way around, and 4 bytes by using the trick of using T which is initialized at 1. \$\endgroup\$ – Robin Ryder Apr 16 at 15:32
  • 1
    \$\begingroup\$ @RobinRyder you might need a +T at the end to make sure that when y==1 it returns 1 instead of TRUE but I'm not entirely sure. \$\endgroup\$ – Giuseppe Apr 16 at 15:35
  • 3
    \$\begingroup\$ @Giuseppe Well spotted! You are right. That makes it 47 bytes \$\endgroup\$ – Robin Ryder Apr 16 at 15:37
  • 1
    \$\begingroup\$ Seems to fail on n=61 (the silly large test case) due to big number issues. I think it's fine to allow for language limits, just noting the exception. \$\endgroup\$ – CriminallyVulgar Apr 17 at 11:33

Jelly, 40 bytes


Try it online!

An alternative Jelly answer, less golfy but more efficient algorithmically when x and y are large. This finds the convergents of the regular continued fraction that approximate the square root of n, and then checks which solve the Pell equation. Now correctly finds the period of the regular continued fraction.

Thanks to @TimPederick, I’ve also implemented an integer-based solution which should handle any number:

Jelly, 68 bytes


Try it online!

For example, the solution for 1234567890 has 1936 and 1932 digits for the numerator and denominator respectively.

  • \$\begingroup\$ Nice! I took the same approach in my answer. I don't read Jelly, so I'm not sure why you're having problems with 61. Are you storing each convergent as a pair of integers (numerator and denominator)? \$\endgroup\$ – Tim Pederick Apr 16 at 19:26
  • \$\begingroup\$ @TimPederick Yes. Not sure where the issue arises \$\endgroup\$ – Nick Kennedy Apr 16 at 19:39
  • \$\begingroup\$ I tried learning how this works so I could help debug it, but I just couldn't wrap my head around it! Only thing I can suggest is taking the floor of any floats, since (if this does use the same algorithm as mine) all intermediate values should come out as integers anyway. \$\endgroup\$ – Tim Pederick Apr 17 at 7:35
  • \$\begingroup\$ @TimPederick It was floating point inaccuracy. I’ve now made it stop looking for further continuation of the continued fraction once it reaches the period. This works up to 150, but above that I think again I run into floating point accuracy errors at e.g. 151 \$\endgroup\$ – Nick Kennedy Apr 17 at 13:15
  • \$\begingroup\$ @TimPederick also it’s the generation of the continued fraction that’s problematic, not the convergents which are done with integer arithmetic. \$\endgroup\$ – Nick Kennedy Apr 17 at 13:16

JavaScript (ES7), 47 bytes


Try it online!

Below is an alternate 49-byte version which keeps track of \$x²-1\$ directly instead of squaring \$x\$ at each iteration:


Try it online!

Or we can go the non-recursive way for 50 bytes:


Try it online!


TI-BASIC,  44  42 41 bytes

Ans→N:"√(N⁻¹(X²-1→Y₁:1→X:Repeat not(fPart(Ans:X+1→X:Y₁:End:{X,Ans

Input is \$n\$.
Output is a list whose values correspond to \$(x,y)\$.

Uses the equation \$y=\sqrt{\frac{x^2-1}{n}}\$ for \$x\ge2\$ to calculate the fundamental solution.
The current \$(x,y)\$ pair for that equation is a fundamental solution iff \$y\bmod1=0\$.


           {5 2}
          {19 6}
       {649 180}


Ans→N:"√(N⁻¹(X²+1→Y₁:1→X:Repeat not(fPart(Ans:X+1→X:Y₁:End:{X,Ans  ;full logic

Ans→N                                                              ;store the input in "N"
      "√(N⁻¹(X²+1→Y₁                                               ;store the aforementioned
                                                                   ; equation into the first
                                                                   ; function variable
                     1→X                                           ;store 1 in "X"
                         Repeat not(fPart(Ans          End         ;loop until "Ans" is
                                                                   ; an integer
                                              X+1→X                ;increment "X" by 1
                                                    Y₁             ;evaluate the function
                                                                   ; stored in this variable
                                                                   ; at "X" and leave the
                                                                   ; result in "Ans"
                                                           {X,Ans  ;create a list whose
                                                                   ; values contain "X" and
                                                                   ; "Ans" and leave it in
                                                                   ; "Ans"
                                                                   ;implicitly print "Ans"

Note: TI-BASIC is a tokenized language. Character count does not equal byte count.


MATL, 17 bytes


Try it online!


The code keeps increasing a counter k = 1, 2, 3, ... For each k, solutions x, y with 1 ≤ xk, 1 ≤ yk are searched. The process when some solution if found.

This procedure is guaranteed to find only one solution, which is precisely the fundamental one. To see why, note that

  1. Any solution x>0, y>0 for n>1 satisfies x>y.
  2. If x,y is a solution and x',y' is a different solution then necessarily xx' and yy'.

As a consequence of 1 and 2,

  • When the procedure stops at a given k, only one solution exists for that k, because if there were two solutions one of them would been found earlier, and the process would have stopped with a smaller k.
  • This solution is the fundamental one, because, again, if there were a solution with smaller x it would have been found earlier.

`       % Do...while
  @:U   %   Push row vector [1^2, 2^2, ..., k^2] where k is the iteration index
  t!    %   Duplicate and transpose. Gives the column vector [1^2; 2^2; ...; k^2]
  G*    %   Multiply by input n, element-wise. Gives [n*1^2; n*2^2; ...; n*k^2]
  -     %   Subtract with broadcast. Gives a square matrix of size n
  !     %   Transpose, so that x corresponds to row index and y to column index
  1=&f  %   Push row and column indices of all entries that equal 1. There can
        %   only be (a) zero such entries, in which case the results are [], [],
        %   or (b) one such entry, in which case the results are the solution x, y
  ts~   %   Duplicate, sum, negate. This gives 1 in case (a) or 0 in case (b)
        % End (implicit). Proceed with next iteration if top of the stack is true;
        % that is, if no solution was found.
        % Display (implicit). The stack contains copies of [], and x, y on top.
        % The empty array [] is not displayed

Python 2, 49 bytes

while x%a*x>1:x+=1
print x,x//a

Try it online!

Finds x as the smallest number above 1 where x % sqrt(n) <= 1/x. Then, finds y from x as y = floor(x / sqrt(n)).


Haskell, 46 bytes

A straightforward brute force search. This makes use of the fact that a fundamental solution \$(x,y)\$ satisfying \$x^2 - ny^2 = 1 \$ must have \$y \leq x\$.

f n=[(x,y)|x<-[1..],y<-[1..x],x^2-n*y^2==1]!!0

Try it online!

  • \$\begingroup\$ It seems like you need to change n to x in y<-[1..n] so you can compute f 13. \$\endgroup\$ – Christian Sievers Jul 24 at 11:05
  • \$\begingroup\$ @ChristianSievers Thanks for pointing it out, I corrected it! \$\endgroup\$ – flawr Jul 26 at 15:47

C# (Visual C# Interactive Compiler), 70 69 bytes

n=>{int x=1;var y=.1;for(;y%1>0;)y=Math.Sqrt(-x*~++x/n);return(x,y);}

Port of my Java 8 answer, but outputs a tuple instead of a string to save bytes.

Try it online.


Jelly, 15 bytes


Try it online!

A full program that takes a single argument n and returns a tuple of x, y.


Husk, 12 bytes


Try it online!


ḟΛ¤ȯ=→*⁰□π2N  Input is n, accessed through ⁰.
           N  Natural numbers: [1,2,3,4,..
         π2   2-tuples, ordered by sum: [[1,1],[1,2],[2,1],[1,3],[2,2],..
ḟ             Find the first that satisfies this:
 Λ             All adjacent pairs x,y satisfy this:
  ¤     □       Square both: x²,y²
   ȯ  *⁰        Multiply second number by n: x²,ny²
     →          Increment second number: x²,ny²+1
    =           These are equal.

MathGolf, 12 bytes


Try it online!

I'm throwing a Hail Mary when it comes to the output formatting. If it's not allowed, I have a solution which is 1 byte longer. The output format is x.0y, where .0 is the separator between the two numbers.


ö       ▼      do-while-true with popping
 k             read integer from input
  î²           index of current loop (1-based) squared
    *          multiply the two
     )         increment (gives the potential x candidate
      _        duplicate TOS
       °       is perfect square
         Þ     discard everything but TOS
          √    square root
           î   index of previous loop (1-based)

I took some inspiration from Emigna's 05AB1E answer, but was able to find some improvements. If the separator I chose is not allowed, add a space before the last byte for a byte count of 13.


APL(NARS), 906 bytes

r←sqrti w;i;c;m

r←pell w;a0;a;p;q2;p2;t;q;P;P1;Q;c;m
   r←⍬⋄→0×⍳w≤0⋄a0←a←sqrti w⋄→0×⍳a≡⍬⋄m←⎕ct⋄⎕ct←0⋄Q←p←1⋄c←P←P1←q2←p2←0⋄q←÷a
L: t←p2+a×p⋄p2←p⋄p←t
   :if c≠0⋄q2←q⋄:endif
   :if c=10000⋄r←⍬⋄:endif
Z: ⎕ct←m

Above there are 2 functions sqrti function that would find the floor square root and pell function would return Zilde for error, and is based reading the page http://mathworld.wolfram.com/PellEquation.html it would use the algo for know the sqrt of a number trhu continue fraction (even if i use one algo for know sqrt using newton method) and stop when it find p and q such that



  ⎕fmt pell 1x
│ 0│
  ⎕fmt pell 2x
│ 3 2│
  ⎕fmt pell 3x
│ 2 1│
  ⎕fmt pell 5x
│ 9 4│
  ⎕fmt pell 61x
│ 1766319049 226153980│
  ⎕fmt pell 4x
│ 0│
  ⎕fmt pell 7373x
│ 146386147086753607603444659849 1704817376311393106805466060│
  ⎕fmt pell 1000000000000000000000000000002x
│ 1000000000000000000000000000001 1000000000000000│

There is a limit for cycles in the loop in sqrti function, and a limit for cycles for the loop in Pell function, both for the possible case number are too much big or algo not converge... (I don't know if sqrti converge every possible input and the same the Pell function too)


Groovy, 53 bytes

n->x=1;for(y=0.1d;y%1>0;)y=((++x*x-1)/n)**0.5;x+" "+y

Try it online!

Port of Kevin Cruijssen's Java and C# answers


Pyth, 15 bytes

fsIJ@ct*TTQ2 2J

Try it online here. Output is x then y separated by a newline.


Wolfram Language (Mathematica), 41 bytes


is the 3-byte Unicode character #221A. Outputs the solution in the order (y,x) instead of (x,y). As usual with the imperfect //. and its limited iterations, only works on inputs where the true value of y is at most 65538.

Try it online!


><>, 45 bytes

$  naon;>

Try it online!

Brute force algorithm, searching from x=2 upwards, with y=x-1 and decrementing on each loop, incrementing x when y reaches 0. Output is x followed by y, separated by a newline.


C# (Visual C# Interactive Compiler), 69 bytes

n=>{for(int x=2,y;;x++)for(y=0;y<=x;y++)if(x*x-y*y*n==1)return(x,y);}

Try it online!


Python 3, 75 bytes

lambda i:next((x,y)for x in range(2,i**i)for y in range(x)if~-x**2==i*y**2)

Try it online!


Brute force. Using $$x<i^i$$ as an upper search bound, which is well below the definite upper limit of the fundamental solution to Pell's equation $$x\leq i!$$

This code would also run in Python 2. However, the range() function in Python 2 creates a list instead of a generator like in Python 3 and is thus immensely inefficient.

With inifinte time and memory, one could use a list comprehension instead of the iterator and save 3 bytes like so:

Python 3, 72 bytes

lambda i:[(x,y)for x in range(i**i)for y in range(x)if~-x**2==i*y**2][1]

Try it online!


Python 2, 64 bytes

f=lambda n,x=2,y=1:x*x-n*y*y-1and f(n,x+(x==y),y*(y<x)+1)or(x,y)

Try it online!

Returns (x, y).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.