Processing math: 100%
Skip to main content

Section 16.2 General Quadratic Congruences

The equation x2+k is not the only quadratic game in town. What about other quadratics, such as x2+2x+1? It turns out that we can use something you are already familiar with to reduce the whole game to the following.

Question 16.2.1.

For what primes p is there a solution to x2≑k (mod p)?

Let's confirm this with a look at general quadratic congruences.

First let's try computing. As an example, take x2βˆ’2x+3 (mod 9). The Sage function solve_mod works, if a little naively.

Sage note 16.2.2. Commands of more sophistication.

Notice that the solve_mod command is more complicated than divmod. solve_mod returns a list of tuples, where a tuple of length one has a comma to indicate it's a tuple. (If you tried to solve a multivariate congruence you would find it returns a longer tuple.)

The result shows that x2βˆ’2x+3≑0 (mod 9) has two solutions. But how might I solve a general quadratic congruence?

Subsection 16.2.1 Completing the square solves our woes

The key is completing the square! First let's do an example.

Example 16.2.3.

Completing the square for x2βˆ’2x+3 is done by

x2βˆ’2x+3=(x2βˆ’2x+(22)2)+3βˆ’(22)2=(xβˆ’1)2+2,

so solving the original congruence reduces to solving

(xβˆ’1)2β‰‘βˆ’2 (mod n)

Then assuming I have a square root s of βˆ’2 (mod n), I just compute s+1 and I'm done! Go ahead and try this for a few different n, including of course n=9, with Sage.

Should you not have particularly enjoyed completing the square in the past, here is the basic idea for modulus n.

Example 16.2.6.

Let's do all this with x2+3x+5≑0 (mod n). Notice that b2βˆ’4ac=9βˆ’20=βˆ’11, so this equation does not have a solution over the integers, or indeed over the real numbers. Does it have a solution in Zn for some n, though?

In the previous example, note that we could not proceed as over the rational numbers by writing

x2+3x+5=(xβˆ’32)2+(5βˆ’(32)2)

since there is no element 3/2∈Zn; this motivates part of the multiplication by 4a in Algorithm 16.2.4.