Integer and Rational Solutions of a Binary Quadratic Equation (5): Binary quadratic form
In this post, we are interested in finding all integer solutions of the following equation:
where , . This equation can certainly be solved using methods from the generalized Pell equation, but here, we explore other methods.
The function is also called the binary quadratic form (BQF), which we denote as for brevity. The discriminant of is defined as .
Theory of binary quadratic forms
Binary quadratic forms have been studied for centuries and many textbooks discuss them. The goal of this section is to provide a brief introduction. Advanced topics (e.g., composition) will not be discussed.
(Equivalence) We define an equivalence relation between BQFs: if and only if there exists an such that and
The matrix determinants of both sides show that and have the same discriminant.
(Representing numbers) One way to understand this equivalence is by examining the set of numbers it represents. We say represents if the equation for some , and it properly represents if . If and represents by , then represents through , which is a simple change of variables (under the constraint that ).
(Positive definite) A BQF is positive definite if for any , . An alternative definition states that and .
(Lagrange-reduced form) A positive definite BQF is (Lagrange-)reduced if it meets both of the following conditions: (1) , (2) if or , then . Every positive definite BQF is equivalent to a unique reduced BQF.
(Reduction) We have two rules to reduce a positive definite BQF, similar to the Euclidean algorithm:
and for any ,
A special case of \eqref{R.2} is for .
We can always apply \eqref{R.1} to ensure that , and \eqref{R.2} to ensure by setting . Both \eqref{R.1} and \eqref{R.2} reduces either or while keeping the other the same, so the process must terminate with a BQF where . Finally, if or , we choose the one with .
See William Stein’s lecture notes and [Manguba-Glover18, Chapter 2] for more details.
Normalization
(Normalization) The equation is first normalized as follows:
- Assume . Otherwise, we divide both sides by .
- Assume . Otherwise, we can always find such that and , and then apply to . See here for how to find . The high level idea is that for any prime , it’s enough to try , and we use CRT to combine the results.
(Primitive solutions) We only care about the primitive solutions where . If , we find the primitive solutions of . It’s also clear that for primitive solutions .
Ellipse case ()
(This section is mostly taken from [Matthews15].)
We assume the BQF is positive definite. Otherwise, we negate and .
For a primitive solution to , define , which certainly satisfies . We write , hence
where
Note that these two BQFs have the same discriminant.
The theorem below determines the minimum positive value of a reduced positive definite BQF:
For a reduced positive definite BQF , the minimum positive value of for is , and is achieved when is:
- if ;
- if ;
- if .
After reducing to using some , we can use the theorem above to determine the solutions of .
Here is an algorithm to solve the case:
def solve_bqf_neg_disc_primitive(a, b, c, n):
"""find primitive solutions to ax^2 + bxy + cy^2 = n.
Assume gcd(a, b, c) = 1 and gcd(c, n) = 1, b^2 - 4ac < 0.
"""
Δ = b*b - 4*a*c
assert Δ < 0
for λ in sqrtmod_all(Δ, n):
λ = invmod(2*a, n) * (λ - b) % n
(a2, b2, c2), M = bqf_neg_disc_reduce((a*n, b - 2*a*λ, (a*λ*λ + b*λ + c) // n))
if a2 != 1:
continue
# transform back to the original equation
M = [[n, -λ], [0, 1]] @ M
if a2 < c2:
sols = [[1, 0], [-1, 0]]
elif b2 < a2:
sols = [[1, 0], [-1, 0], [0, 1], [0, -1]]
else:
sols = [[1, -1], [-1, 1]]
yield from (M @ s for s in sols)
Parabola case ()
(This section is mostly taken from [Matthews15] and [MRS17].)
Structure and transformation
(Equivalence relation) [Matthews23, Section 2] Let be the fundamental solution to , and be a solution to , then any such that
for is also a solution to . Hence we can use the associated equivalence relation from the group action of a group on the set of all solutions, which is the Stolt equivalence.
(Partition of solution space) Similarly as the ellipse case, we define for a primitive solution and transform to . One can also show ([Stolt57, Theorem 5]) that two primitive solutions are Stolt equivalent if and only if they have the same , implying that the number of Stolt equivalent classes is the finite.
Find Stolt fundamental solutions
Our goal in this subsection is to find the Stolt fundamental solution for a specific , which solves the equation
with minimum non-negative (and maximum for tie-breaking).
With abuse of notation, we study the equation where is not a square, and . Additionally, we require . Since and is not a square, we have .
We first present a very interesting result in [Serret66]:
([Serret66]) Let , and let be a primitive solution to with where . In addition, let be the roots of where . Then is a convergent to either or .
[MRS17] gives a simple proof: Note . If ,
then by Dirichlet’s approximation theorem, we conclude that is a convergent to . Similarly, if , it’s a convergent to .
However, the theorem is no longer true for negative . [Pavone86] generalized the theorem for
where he proved that Serret’s theorem still holds unless , , in which case can also be a special semiconvergent of both and . [MRS17] further generalizes [Pavone86]‘s result, allowing .
Anyway, [Pavone86]‘s result is enough for us since we only care about the case . One way to find the fundamental solution is to examine the candidates from the following categories to see if they solve the equation, and pick the most fundamental one:
- Check the smallest convergent of before the first period ends;
- Check the smallest convergent of before the first period ends;
- If and , also check the special semi-convergent.
[Matthews02] also designed an algorithm based on [Pavone86], but added many optimizations.
References
General:
- Robertson09: Robertson, J., 2009. Computing in quadratic orders. at jpr2718. org.
Ellipse case ():
- Matthews15: Matthews, K.R., 2015. On a transformation of Lagrange [online]
- Matthews14: Matthews, K.R., 2014. Lagrange’s Algorithm Revisited: Solving in the case of negative discriminant. Journal of Integer Sequences, 17(11).
- It uses continued fraction and is more complicated. Also need to handle a few exceptional cases.
- Manguba-Glover18: Manguba-Glover, K., 2018. Reduction Theory of Binary Quadratic Forms (Doctoral dissertation, University of Hawai ‘i at Manoa).
Parabola case ():
- Matthews23: Matthews, K., 2023. Finding the Fundamental Solutions of .
- Notes on Stolt equivalence.
- MR21: Matthews, K.R. and Robertson, J.P., 2021. On solving a binary quadratic Diophantine equation. Rocky Mountain Journal of Mathematics, 51(4), pp.1369-1385.
- Unfortunately it’s paywalled so I haven’t read it yet. My guess is that it’s pretty similar to [Matthews23].
- Matthews02: Matthews, K., 2002. The diophantine equation , . Journal de théorie des nombres de Bordeaux, 14(1), pp.257-270.
- Continued fraction-based algorithm.
- MRS15: Matthews, K.R., Robertson, J.P. and Srinivasan, A., 2015. On fundamental solutions of binary quadratic form equations.
- This bounds the scale of the Stolt fundamental solutions.
- MRS17: Matthews, K.R., Robertson, J.P. and Srinivasan, A., 2017. Extending Theorems of Serret and Pavone. J. Integer Seq., 20(10), pp.17-10.
- [Serret66] : Serret, J.A., 1866. Cours d’algèbre supérieure (Vol. 1). Gauthier Villars.
- Pavone86: Pavone, M., 1986. A Remark on a Theorem of Serret. Journal of Number Theory, 23(2), pp.268-278.
- Stolt57: Stolt, B., 1957. On a Diophantine equation of the second degree. Arkiv för Matematik, 3(4), pp.381-390.