In this post, we’re interested in finding any or all integer solutions of a binary quadratic equation:
x2−ny−a=0.
We assume that the equation is non-degenerate in the sense that n=0.
Normalization
We first normalize the problem to simplify its properties.
Assume n>0. Otherwise, reduce it to x2−(−n)(−y)−a=0.
Assume n=pe is a prime power. Otherwise, write n=i=1∏kpiei and solve x2≡a(modpiei) for each piei and use CRT to combine all results.
Assume a=0. If a=0, all solutions are {pimodpe:e≤2i≤2e}.
Assume p does not divide a. Otherwise, write a=a′pe′ where p does not divide a′.
If e′ is odd, there exists no solution.
If e′ is even, x must be a multiple of pe′/2, so reduce it to (x/pe′/2)2≡a′(modpe−e′).
After normalization, the problem that we’re interested in is:
Default
For a prime p, an integer a=0 such that p does not divide a, find one or all integer x such that
x2≡a(modpe).\labeleq:quad−residue
We say that x is a quadratic residue modulo n if x2≡a(modn) has a solution, and a quadratic non-residue modulo n otherwise.
The case of p=2
(Solubility) For p=2, a is a quadratic residue modulo pe if and only if a≡1(mod8).
To find one or all solutions, we first consider the two easy cases:
If e=1, certainly the case is a≡1(mod2) and all solutions are x≡1(mod2).
If e=2:
If a≡1(mod4), all solutions are x≡1(mod2).
Otherwise, there is no solution.
Now we assume e>2.
(Any solution) Observe that:
(x+2x2−a)2−a=(x+1)(x2−a)+(2x2−a)2.
So if xe−1 satisfies x2≡a(mod2e−1), then xe=xe−1+2xe−12−a satisfies x2≡a(mod2e). Thus, we can begin with x2=1.
(All solutions) Let x∗ be any solution to x2≡a(mod2e). It is clear that x∗ must be odd. Consider 2x∗−x and 2x∗+x. The sum is x∗, so only one of them is even. The product is 4x∗2−x2≡0(mod2e−2), thus one of them is 0 modulo 2e−2. Thus, the set of all solutions is:
x∈{±x∗}(mod2e−1).
The case of an odd prime p
(Solubility) The solubility is often determined by the Jacobi symbol, which is the generalization of the Legendre symbol: For an odd prime p, a is a quadratic residue modulo pe if and only if
(pea)=1,(pea):=(pa)e,
where (pa) is the Legendre symbol.
Note that this does not apply to p=2 (e.g., x2≡3(mod4)) or general composite numbers (e.g., x2≡8(mod15)).
(Modulo a prime power (e>1)) Let x′ be any solution to x2≡a(modp). Tonelli shows that
x∗≡x′pe−1⋅a(pe−2pe−1+1)/2(modpe),
is a solution to Equation \eqref{eq
}.
Alternatively, Cipolla’s algorithm can be extended to find a solution to Equation \eqref{eq
} directly: Let u be any integer such that u2−a is a quadratic non-residue modulo p, then
x∗≡21a(pe−2pe−1+1)/2((u+u2−a)s+(u−u2−a)s).(modpe)
where s:=pe−1(p+1)/2.
Moreover, it’s also possible to use the identity
(2xx2+a)2−a=4x2(x2−a)2
to generate a solution from x′ by iterating it for loge times, but a division modulo pe takes O(logpe) time so it won’t be faster.
(All solutions) Similarly, let x∗ be any solution to x2≡a(modpe). Since (x∗−x)(x∗+x)≡0(modpe), and at most one of x∗−x and x∗+x is a multiple of p, we conclude that all solutions are