Integer and Rational Solutions of a Binary Quadratic Equation (4): Pell equation and generalized Pell equation
In this post, we are interested in finding all integer solutions of Pell equation
and generalized Pell equation
for a non-square integer and .
Connection to
For any element in , we define . It is easy to show that for any .
Note that for , Now we define
so solving Pell equations is equivalent to calculating and solving generalized Pell equations is equivalent to calculating .
Most references I found online use the notation, but in certain cases, it might be cleaner to present the results using operations in , especially when presenting the structure of solutions.
Primitive solutions
(Primitive solutions) A solution is primitive if . If is primitive, .
Similar to solving , once we have an algorithm to find all primitive solutions, we can derive all solutions. Let be the set of all primitive solutions of :
then it is straightforward to see that
Clearly, .
Pell equation
The Pell equation has been studied extensively over time. See [Lenstra02] for a comprehensive overview. There are so many tutorials (e.g. [Conrad22A] and [Conrad22B]) on solving Pell equations, so here I only list a few key conclusions and the algorithm.
(Structure of solutions) It follows from Dirichlet’s unit theorem that has the structure
for a unique fundamental solution where . Note can be negative, as . See [Conrad22A, Theorem 5.3] for proof.
(Fundamental solution). It is also shown that must be a convergent of . We can use the PQa algorithm below to find all convergents. See [Conrad22B, Theorem 5.1] for proof.
(PQa algorithm) Given and such that , the PQa algorithm computes the continued fraction of . It works as follows:
Note that the division for calculating is always exact.
Below is an implementation of the PQa algorithm optimized for solving the Pell equation. See [Robertson04] for more details.
def pell(d):
"""find the fundamental solution of x^2 - dy^2 = 1 and -1"""
p, q = 0, 1
sqrt_d = int(d ** 0.5)
x0, x1 = 0, 1
y0, y1 = 1, 0
l = 0
while l == 0 or q != 1:
a = (p + sqrt_d) // q
p = a * q - p
q = (d - p ** 2) // q
l += 1
x0, x1 = x1, a * x1 + x0
y0, y1 = y1, a * y1 + y0
if l % 2 == 1:
pos = x1 * x1 + y1 * y1 * d, x1 * y1 * 2
neg = x1, y1
else:
pos = x1, y1
neg = None
return pos, neg
Generalized Pell equation
(The idea in this section is taken from [Matthews00].)
(Nagell equivalence) For any and , we observe
implying that . This motivates us to define equivalence relation, the Nagell equivalence, for elements in : Two solutions if and only if there exists an element , such that .
(Partition of solution space) Let and be two primitive solutions, and define . Since , it follows that
So an alternative definition of Nagall equivalence for primitive solutions is that if and only if .
(Structure of solutions) The alternative definition of Nagall equivalence directly implies the number of equivalent classes in is finite, that is, there exists a finite set such that
where is the fundamental solution of .
(Finding a minimum positive solution) Now we focus on one equivalence class, represented by (with abuse of notation) where . If we write , it is proved in [Matthews00, Theorem 1] that is a convergent of . Thus, we can find a minimum positive solution with smallest positive and using the PQa algorithm.
(Finding a fundamental solution) It might be more convenient to find a fundamental solution for an equivalence class, with smallest positive . If multiple solutions have the same smallest positive , choose the one with positive . A fundamental solution can be found as follows:
- If , the minimum positive solution is the fundamental solution;
- Otherwise, let and be the minimum positive solutions of and . If , the fundamental solutions are and ; otherwise they’re and .
The algorithm is almost the same as [Mollin01, Algorithm 4.1]. The Lagrange-Matthews-Mollin (LMM) algorithm, proposed in [Matthews00, Section 5], improves the process by considering properties of the continued fraction of .
Here is the pseudocode:
def solve_generalized_pell_primitive(d, n):
"""find all fundamental solutions of x^2 - d y^2 = n"""
for λ in sqrtmod_all(d, n):
if (s := PQa(d, λ, n)) is None:
continue
if λ == 0 or λ * 2 == n:
yield s
elif λ * 2 < n:
p1, q1 = s
p2, q2 = PQa(d, -λ, n)
if q1 < q2:
yield from (p1, q1), (-p1, q1)
else:
yield from (p2, q2), (-p2, q2)
A special generalized Pell equation
We also discuss special generalized Pell equation , as it also shares many properties as the Pell equation .
As we have mentioned above, the set has a special structure for a fundamental solution . Similarly, the solutions for have a similar structure
for a fundamental solution where is minimum positive solution to [Stolt57, p.4]. There is actually a connection between and : for ([Stolt57, p.4], [Matthews23, Theorem 1.1]).
(Equivalence relation) Observing that Negall equivalence is the associated equivalence relation of the group action of group on the set . As is also a group, we can define a similar equivalence relation, the Stolt equivalence. In some sense, the Stolt equivalence is more fundamental than the Negall equivalence, in the sense that can be generated by but might not be generated by . As a consequence, the Stolt equivalence might have fewer equivalence classes than the Negall equivalence.
To find the fundamental solution , check out [Johnson04] for an efficient algorithm implemented below:
def pell4(d):
"""find the mininum positive solution to x^2 - d y^2 = 4 or -4"""
if d % 4 == 1:
p, q = 1, 2
sqrt_d = int(d ** 0.5)
x0, x1 = -1, 2
y0, y1 = 1, 0
l = 0
while l == 0 or q != 2:
a = (p + sqrt_d) // q
p = a * q - p
q = (d - p ** 2) // q
l += 1
x0, x1 = x1, a * x1 + x0
y0, y1 = y1, a * y1 + y0
if l % 2 == 1:
pos = (x1 * x1 + y1 * y1 * d) // 2, x1 * y1
neg = x1, y1
else:
pos = x1, y1
neg = None
elif d % 4 == 0:
pos, neg = pell(d // 4)
pos = (pos[0] * 2, pos[1])
if neg:
neg = (neg[0] * 2, neg[1])
else:
pos, neg = pell(d)
pos = (pos[0] * 2, pos[1] * 2)
if neg:
neg = (neg[0] * 2, neg[1] * 2)
return pos, neg
References
-
Rathnayake13: Rathnayake T., 2013. Quadratic Diophantine equation – I [online]
-
Lenstra02: Lenstra Jr, H.W., 2002. Solving the Pell equation. Notices of the AMS, 49(2), pp.182-192.
-
Conrad22A: Conrad, K., 2022. Pell’s equation, I [online]
-
Conrad22B: Conrad, K., 2022. Pell’s equation, II [online]
-
Matthews00: Matthews, K., 2000. The Diophantine Equation , . Expositiones Mathematicae, 18(4), pp.323-332.
-
Tamang21: Tamang, B.B., 2021. On the study of quadratic Diophantine equations (Doctoral dissertation, Tribhuvan University Kathmandu).
- Discussed this topic in more details and shows more examples.
-
Mollin01: Mollin, R.A., 2001. Simple continued fraction solutions for Diophantine equations. Expositiones Mathematicae, 19(1), pp.55-73.
-
Robertson04: Robertson, J.P., 2004. Solving the generalized Pell equation . Unpublished manuscript.
- It gives a complete description of the LMM algorithm.
-
Stolt57: Stolt, B., 1957. On a Diophantine equation of the second degree. Arkiv för Matematik, 3(4), pp.381-390.
Update @2025-03-20: Added the discussion of and the optimized algorithm for the Pell equation.