Integer and Rational Solutions of a Binary Quadratic Equation (0): General binary quadratic equation
In this post, we are interested in finding all integer and rational solutions for a general binary quadratic equation:
for any integer coefficients . The equation might degenerate; we only point it out when it does but do not solve it, as it’s typically easier to solve a degenerate equation.
This post mainly serves as an entry point for a series of posts and also contains a little bit of the story behind the posts. That is why this post has index 0 in the title.
The story
As readers might know, I have been practicing Project Euler for years. Project Euler contains many problems related to number theory, some of which involve solving binary Diophantine equations. One of the most famous examples of quadratic Diophantine equations is the Pythagorean triples ().
One day, I suddenly had an idea: What’s the general method for solving quadratic equations? How can we enumerate the solutions? Given that there might be infinite solutions, probably we want to enumerate bounded solutions efficiently (mostly for solving Project Euler problems). So I spent time researching, reading numerous papers, notes, books and blog posts… Until I thought I was done and ready to summarize what I had learned into posts.
While finishing my writing, I found Quadratic diophantine equations BCMATH programs, and immediately realized that my literature review was so incomplete. The website covers most related topics and is maintained by Keith Matthews, who seems to be a domain expert, with regular updates. Reading his papers made me question the necessity of my posts…
Nevertheless, I decided to proceed with my writing. Perhaps it’s for my own understanding, or perhaps I simply don’t want to admit I haven’t published anything on my blog for two months (so I even break the topic down into many small parts)…
What’s next? I still have two incomplete things, but it depends on whether my interests fade.
- Implement the algorithms, which is a perfect chance to learn a new programming language, just like what I did for my previous side project (Analytic prime counting, Part 1 and Part 2).
- More efficient enumeration of primitive solutions of the Legendre equation. Although an upper bound of the GCD exists, I still want to make it more efficient by removing GCD. The requirement of enumerating the primitive input pair probably can’t be removed, though.
Common techniques
Suppose we want to solve .
- (Homogenization) If is not homogeneous, it may be beneficial to homogenize it;
- (Diagonalization) If is quadratic but is not diagonal, diagonalize it;
- (Primitive solutions) Only consider the primitive solutions. For most cases, an algorithm for primitive solutions can be easily used to find non-primitive solutions (Turing reduction);
- (Partition of solution space) Consider the equation . The solution gives us strong constraints on , which is enough to partition the solution space.
Integer solutions
Let be the discriminant of the equation.
-
(Case 1) If :
- (Case 1.1) If : it simplifies to a linear case ;
- (Case 1.2) If : This results in a degenerate case: . To solve, factor to express it in terms of and .
-
(Case 2) If while or : Without loss of generality (W.L.O.G.), we assume . Note
Thus:
- (Case 2.1) If : Solve ;
- (Case 2.2) If : Solve using quadratic residue.
-
(Case 3) If , while or . W.L.O.G. we assume . Note
where
Thus:
- (Case 3.1) If and is not a perfect square, we solve the generalized Pell equation ;
- (Case 3.2) If and is a square, we solve .
- (Case 3.3) If , we solve the ellipse case .
-
(Alternative Case 3) If , note
where
Then we can use the methods about solving binary quadratic forms to find all integer solutions to . The advantage of the method is that we don’t need to diagonalize the equation, although many methods implicitly include this step.
Note that many changes of variable are not unimodular, so we need to verify whether the result is an integer solution.
Rational solutions
We write the equation in the matrix form:
(Diagonalization) The matrix may not be diagonal. If so, we can diagonalize it by finding an invertible matrix and a diagonal matrix such that
It follows that
One possible approach is to use the LDL decomposition. However, the matrix may not have a LDL decomposition if, at some step (e.g., ), the diagonal element becomes zero. If this occurs, we can conclude that the equation at that step is actually linear in the variable (e.g., in the example). Thus we can always treat other variables () as free variables and compute in the end.
(Legendre equation) Let , we consider the primitive solutions to the Legendre equation . For any primitive solution to the Legendre equation, write . If , then is a rational solution to the original equation.
(Solving Legendre equation) For the Legendre equation , we assume W.L.O.G. We analyze the number of zero coefficients among .
- There are 3 zeros. Any for any is a solution;
- There are 2 zeros. Any for any is a solution;
- There is 1 zero. Any for any , and any rational solution to , is a solution.
- There is no zero. We find any non-trivial integer solution to the Legendre equation , and every rational solution can be parameterized by .
Homework:

Answer: See Alon Amit’s excellent post.
References
General resources from the Internet:
-
Quadratic diophantine equations BCMATH programs by Keith Matthews.
- Also check out the history of his algorithm for his line of work.
-
Methods to solve quadratic Diophantine equations by Dario Alejandro Alpern;
- The validity of the following statement is uncertain:
If , the solutions of the equation will be amongst the convergents of the continued fraction of the roots of the equation .
-
Sander Verdonschot’s repo, which implements many Keith Matthews’ papers in Java;
-
Thilina Rathnayake’s blog when developing the Diophantine module of SciPy;
-
Writings in Keith Conrad’s websites. It may be elementary, but it is sufficient for amateurs like me.
Textbooks and notes:
- [Dickson19] : Dickson, L.E., 1919. History of the Theory of Numbers (No. 256). Carnegie Institution of Washington.
- [Mordell69] : Mordell, L.J., 1969. Diophantine Equations: Diophantine Equations (Vol. 30)
- Matthews15: Matthews, K., 2015. Solving the Diophantine Equation .