Congruences

=Congruences= The subject of congruences is a field of mathematics that covers the integers, their relationship to each other and also the effect of arithmetic operations on their relationship to each other.

Expressed mathematically:


 * $$A \equiv B \pmod{N}$$

read as: A is congruent with B modulo N.

Programming language python, for example, accepts the following modular arithmetic: On this page we'll say that $$A,B,N$$ are integers and $$N > 1.$$

This means that:


 * A modulo N equals B modulo N,
 * the difference, A-B, is exactly divisible by N, or
 * $$A-B = K\cdot N.$$

where p modulo N or  is the remainder when p is divided by N.

For example: $$23 \equiv 8 \pmod{5}$$ because division $$\frac{23-8}{5}$$ is exact without remainder, or $$5\mid (23-8).$$

Similarly, $$39 \not\equiv 29 \,\pmod{7}$$ because division $$\frac{39-29}{7}$$ is not exact, or $$7\nmid (39-29).$$

Adding a constant
If $$A \equiv B \pmod{N}, $$ then: $$A+q \equiv B+q \pmod{N}.$$

Proof:

$$A-B = K\cdot N$$, therefore $$A = B + K\cdot N.$$

$$(A+q) - (B+q) = B + K\cdot N + q - B - q = K\cdot N$$ which is exactly divisible by N.

Adding 2 congruences
If $$A \equiv B \pmod{N}, $$ and $$C \equiv D \pmod{N}, $$ then: $$A+C \equiv B+D \pmod{N}.$$

Proof:

$$A-B = K_1\cdot N$$, therefore $$A = B + K_1\cdot N$$ and $$C = D + K_2\cdot N$$

$$(A+C) - (B+D)$$ $$= B + K_1\cdot N + D + K_2\cdot N - B - D$$ $$= N(K_1 + K_2)$$ which is exactly divisible by N.

Law of Common Congruence
If $$A \equiv B \pmod{N} $$ and

$$C \equiv B \pmod{N},$$ then:

$$A \equiv C \pmod{N}.$$

Proof:

$$A = B + K_1\cdot N$$ and $$C = B + K_2\cdot N.$$

$$A - C = B + K_1\cdot N - B - K_2\cdot N = (K_1 - K_2)N$$ which is exactly divisible by N.

by a constant
If $$A \equiv B \pmod{N} $$ then:

$$A\cdot p \equiv B\cdot p \pmod{N}.$$

Proof:

$$A\cdot p - B\cdot p = p(A-B)$$ which is exactly divisible by N.

by another congruence
If $$A \equiv B \pmod{N} $$ and

$$C \equiv D \pmod{N},$$ then:

$$A\cdot C \equiv B\cdot D \pmod{N}.$$

Proof:

$$A = B + K_1\cdot N$$ and $$C = D + K_2\cdot N.$$

$$A\cdot C - B\cdot D$$ $$= (B + K_1\cdot N)( D + K_2\cdot N) - B\cdot D$$ $$= B\cdot D + B\cdot  K_2\cdot N + K_1\cdot N\cdot  D + K_1\cdot N\cdot  K_2\cdot N - B\cdot D$$ $$= N( B\cdot K_2  + K_1\cdot   D + K_1\cdot  K_2\cdot N )$$ which is exactly divisible by N.

Law of squares
If $$A \equiv B \pmod{N} $$ then:

$$A^2 \equiv B^2 \pmod{N}.$$

Proof:

$$A^2 - B^2 = (A+B)(A-B)$$ which is exactly divisible by N.

Law of Division?
A simple example shows that a "law of division" does not exist.

$$24 \equiv 14 \pmod{10}.$$

However $$\frac{24}{2} \not\equiv \frac{14}{2} \pmod{10}$$

Because $$12 - 7 = 5$$ is not exactly divisible by $$10$$

If division of operators is performed carefully, division can be very useful.

Factor common to (A, B)
Let $$A \equiv B \pmod{N}$$

If $$A, B$$ share a common factor $$p$$ then:

$$p\cdot a \equiv p\cdot b \pmod{N}.$$

Provided that factor $$p$$ does not divide $$N,$$ then:

$$a \equiv b \pmod{N}.$$

Proof: division $$\frac{p \cdot a - p \cdot b}{N} = \frac{p(a - b)}{N}$$ is exact.

Factor $$p$$ does not divide $$N,$$ therefore division $$\frac{a - b}{N}$$ must be exact.

For example : $$42 \equiv 207 \pmod{55}$$

$$3\cdot 14 \equiv 3\cdot 69 \pmod{55}$$

$$14 \equiv 69 \pmod{55}$$

Factor common to (A, B, N)
If operands $$A, B, N$$ all share a common factor $$p$$ then:

$$p\cdot a \equiv p\cdot b \pmod{(p\cdot n)}$$ in which case:

$$a \equiv b \pmod{n}.$$

Proof: division $$\frac{p(a-b)}{p\cdot n}$$ is exact.

Therefore, division $$\frac{a-b}{n}$$ must be exact.

For example: $$22 \equiv 77 \pmod{55}.$$

Therefore: $$2 \equiv 7 \pmod{5}.$$

=Linear congruences= A linear congruence is similar to a linear equation, except that it is expressed in modular format:

$$A\cdot x \equiv B \pmod{N}$$ where $$A, B, N$$ are integers and $$N > 1.$$

Law of addition
If $$A\cdot x \equiv B \pmod{N},$$ then $$A\cdot x + C \equiv B + C \pmod{N}$$

Proof is similar to that offered above.

Note that the congruence $$(A+C)\cdot x \equiv B+C \pmod{N}$$ is not valid, except for $$C = p\cdot N.$$

Generally: $$(A+p\cdot N)\cdot (x + q\cdot N) \equiv (B+r\cdot N) \pmod{N}.$$

Proof: $$(A+p\cdot N)\cdot (x + q\cdot N) - (B+r\cdot N)$$

$$= A\cdot x + A\cdot q\cdot N + p\cdot N\cdot x + p\cdot N\cdot q\cdot N - B - r\cdot N$$

$$= A\cdot x -B + N\cdot (A\cdot q + p\cdot x + p\cdot N\cdot q - r)$$ which is exactly divisible by $$N.$$

A corollary of this proof is:

$$(A % N)\cdot (x % N) \equiv (B % N) \pmod{N}.$$

Law of multiplication
If $$A\cdot x \equiv B \pmod{N},$$ then $$C\cdot A\cdot x \equiv C\cdot B \pmod{N},$$

Proof: $$C\cdot A\cdot x - C\cdot B $$

$$= C\cdot ( A\cdot x - B) $$ which is exactly divisible by $$N.$$

Simplify the congruence
Simplifying the congruence means reducing the value of $$A$$ as much as possible.

For example: $$30121\cdot x \equiv 30394 \pmod{35893}$$

Three values share a common prime factor $$13.$$

Therefore: $$\frac{30121}{13}\cdot x \equiv \frac{30394}{13} \pmod{\frac{35893}{13}}$$

$$2317\cdot x \equiv 2338 \pmod{2761}$$ Two values share a common prime factor $$7.$$

Therefore: $$\frac{2317}{7}\cdot x \equiv \frac{2338}{7} \pmod{2761}$$

$$331\cdot x \equiv 334 \pmod{2761}$$

Linear congruence $$30121\cdot x \equiv 30394 \pmod{35893}$$ has been reduced to $$331\cdot x \equiv 334 \pmod{2761}.$$

Solve the congruence
Solution of congruence is a value of $$x$$ that satisfies the congruence.

Solving the linear congruence means continuously simplifying the congruence by reducing the value of $$A$$ until $$A$$ becomes $$1$$ at which point the congruence is solved.

Example 1
For example: $$1327\cdot x \equiv -4539 \pmod{29}$$ becomes in turn:

$$22\cdot x \equiv 14 \pmod{29}$$

$$11\cdot x \equiv 7 \pmod{29}$$

$$33\cdot x \equiv 21 \pmod{29}$$

$$4\cdot x \equiv 21 \pmod{29}$$

$$28\cdot x \equiv 147 \pmod{29}$$

$$-1\cdot x \equiv 147 \pmod{29}$$

$$1\cdot x \equiv -147 \pmod{29}$$

$$1\cdot x \equiv 27 \pmod{29}$$

Yes: $$(1327*27 - (-4539))\ %\ 29 = 0.$$

Example 2
For example, solve $$439\cdot x \equiv 2157 \pmod{2693}$$

$$\text{quotient, remainder = divmod(2693, 439)}$$

$$\text{quotient, remainder = 6, 59}$$

$$\text{remainder}\ <=\ (439\ >>\ 1),$$ therefore:

$$\text{A = remainder = 59}$$

$$\text{B} = (-\text{B} * \text{quotient}) % \text{N} = 523$$

$$59\cdot x \equiv 523 \pmod{2693}$$

$$\text{quotient, remainder = divmod(2693, 59)}$$

$$\text{quotient, remainder = 45, 38}$$

$$\text{remainder} > (59 >> 1),$$ therefore:

$$\text{A} = \text{A} - \text{remainder} = 21$$

$$\text{B} = (\text{B}*(\text{quotient}+1)) % \text{N} = 2514$$

$$\text{New value of}\ A$$ is always $$<=\ \frac{\text{Old value of}\ A}{2}.$$

$$21\cdot x \equiv 2514 \pmod{2693}$$

$$7\cdot x \equiv 838 \pmod{2693}$$

$$\text{quotient, remainder = divmod(2693, 7)}$$

$$\text{quotient, remainder = 384, 5}$$

$$\text{remainder} > (7 >> 1),$$ therefore:

$$\text{A} = \text{A} - \text{remainder} = 2$$

$$\text{B} = (\text{B}*(\text{quotient}+1)) % \text{N} = 2163$$

$$2\cdot x \equiv 2163 \pmod{2693}$$

$$\text{quotient, remainder = divmod(2693, 2)}$$

$$\text{quotient, remainder = 1346, 1}$$

$$\text{remainder}\ <=\ (2\ >>\ 1),$$ therefore:

$$\text{A = remainder = 1}$$

$$\text{B} = (-\text{B} * \text{quotient}) % \text{N} = 2428$$

$$1\cdot x \equiv 2428 \pmod{2693}$$

Example 3
Method described here is algorithm used in python code below.

For example, solve the linear congruence:

$$113\cdot x \equiv 263 \pmod{311}\ \dots\ (1).$$

$$113\cdot x = 263 + 311\cdot p\ \dots\ (2)$$

$$311\cdot p -(-263) = 113\cdot x$$

$$311\cdot p \equiv -263 \pmod{113}$$

$$85\cdot p \equiv 76 \pmod{113}$$

$$(113-85)\cdot p \equiv (113-76) \pmod{113}$$

$$28\cdot p \equiv 37 \pmod{113}$$

$$28\cdot p = 37 + 113\cdot q\ \dots\ (3)$$

$$113\cdot q \equiv -37 \pmod{28}$$

$$1\cdot q \equiv 19 \pmod{28}$$

From $$(3):\ p = \frac{37+113\cdot q}{28} = \frac{37+113\cdot 19}{28} = 78$$

From $$(2):\ x = \frac{263+311\cdot p}{113} = \frac{263+311\cdot 78}{113} = 217$$

Check:

Modular Inverses
If you have to perform many calculations with constant $$A, N,$$ it may be worthwhile to calculate $$A\cdot x \equiv 1 \pmod{N}.$$

Let $$A^{'}$$ be a solution of this congruence. Then:

$$A\cdot A^{'} \equiv 1 \pmod{N}.$$

$$A$$ and $$A^{'}$$ are modular inverses because their product is $$\equiv 1 \pmod{N}.$$

For $$A\cdot x \equiv B_0 \pmod{N}.$$

$$A\cdot A^{'}\cdot x \equiv A^{'}\cdot B_0 \pmod{N}.$$

$$1\cdot x \equiv A^{'}\cdot B_0 \pmod{N},$$ and likewise for

$$1\cdot x \equiv A^{'}\cdot B_1 \pmod{N},$$

$$1\cdot x \equiv A^{'}\cdot B_2 \pmod{N},$$

$$1\cdot x \equiv A^{'}\cdot B_3 \pmod{N}.$$

With N composite
When $$N$$ is composite, it may happen that the process of simplifying the congruence $$A\cdot x \equiv B \pmod{N}\ \dots\ (1)$$ produces another congruence $$a\cdot x \equiv b \pmod{n}$$ where division $$\frac{N}{n}$$ is exact.

Let $$x_1$$ be a solution of congruence $$a\cdot x \equiv b \pmod{n}.$$ Then, every solution of this congruence has form $$x_1 + K\cdot n.$$

Solution $$x_1 + K\cdot n$$ is also a solution of congruence $$(1):$$

$$A\cdot (x_1 + K\cdot n) \equiv B \pmod{N}.$$

For example :

$$63\cdot x \equiv 56 \pmod{77}$$

$$9\cdot x \equiv 8 \pmod{11}$$

$$2\cdot x \equiv 3 \pmod{11}$$

$$2\cdot x \equiv 3+11 \pmod{11}$$

$$x \equiv 7 \pmod{11}$$

$$x_1 = 7 + 11\cdot K.$$

python code
During testing on my computer with $$N$$ a random integer containing 10,077 decimal digits, length of stack was 13,514 and time to produce solution was about 13 seconds.

With 2 unknowns
Given: $$21613x + 31013y = 4095605616\ \dots\ (1)$$

Calculate all values of $$x,y$$ for which both $$x,y$$ are positive integers.

Put equation $$(1)$$ in form of congruence:

$$ax + by = s$$

$$-by + s = ax$$

$$-by - (-s) = ax$$

$$-by \equiv -s \pmod {a}$$

$$by \equiv s \pmod {a}$$

$$31013y \equiv 4095605616 \pmod {21613}$$

$$9400y \equiv 6955 \pmod {21613}\ \dots\ (2)$$

Solve congruence $$(2):$$

Put equation $$(1)$$ in form of congruence:

$$ax + by = s$$

$$-ax + s = by$$

$$-ax - (-s) = by$$

$$-ax \equiv -s \pmod {b}$$

$$ax \equiv s \pmod {b}$$

$$21613x \equiv 4095605616 \pmod {31013}$$

$$21613x \equiv 28836 \pmod {31013}\ \dots\ (3)$$

Solve congruence $$(3):$$

From congruence $$(2):\ y = y_1 + pa$$

From congruence $$(3):\ x = x_1 + qb$$

Put these values of $$x,y$$ in equation $$(1):$$

$$a(x_1 + qb) + b(y_1 + pa) = s$$

$$a(x_1) + aqb + b(y_1) + bpa = s$$

$$aqb + bpa = s - (a(x_1) + b(y_1))$$

$$ab(q + p) = s - (a(x_1) + b(y_1))$$

$$p + q = \frac{s - (a(x_1) + b(y_1))}{ab}$$

$$p + q = 4$$

Values of $$x,y$$ highlighted with $$\text{****}$$ are all positive, integer values of $$x,y.$$

With 3 unknowns
Given: $$2200013x + 1600033y + 2800033z = 33420571802161\ \dots\ (1).$$

Solve $$(1)$$ for integer values of $$x,y,z.$$

Put equation $$(1)$$ in form of congruence:

$$2200013x + 1600033y - 33420571802161 = -2800033z$$

$$2200013x + 1600033y \equiv 33420571802161 \pmod {2800033}$$

$$2200013x + 1600033y \equiv 2321520 \pmod {2800033}$$

Let $$2200013x + 1600033y = 2321520\ \dots\ (2).$$

Solve $$(2)$$ for integer values of $$x,y.$$

$$1600033y \equiv 2321520 \pmod {2200013}$$

$$y_1 = 710184$$

$$y = 710184 + p2200013$$

From $$(1):\ 2200013x + 1600033y + 2800033z = 33420571802161$$

$$2200013x + 2800033z = 33420571802161 - 1600033y$$

Using $$y = y_1:$$

$$A\cdot x + C\cdot z = D\_$$ or:

$$2200013x + 2800033z = 32284253966089\ \dots\ (3)$$

Solve $$(3)$$ for integer values of $$x,z.$$

$$x_1 = 2283529$$

$$x = 2283529 + q\cdot C$$

Values of $$x,z$$ highlighted with $$\text{****}$$ are all positive, integer values of $$x,z.$$

This section shows that all calculated points are in fact on same line.

Direction numbers are consistent. Therefore, all points are on same line.

Recall original equation:

$$2200013x + 1600033y + 2800033z = 33420571802161\ \dots\ (1).$$

$$2800033$$ is coefficient of $$z.$$

$$2200013$$ is coefficient of $$x.$$

There is not only an infinite number of points for which all of $$x,y,z$$ are of type integer.

There is also an infinite number of lines similar to the example in this section.

This example used $$y = 710184.$$

However, $$y$$ can be $$710184 + p\cdot 2200013$$ where $$p$$ is any integer.

#2
In this example:


 * Plane $$\pi_1$$ is same as plane $$\pi_1$$ above.


 * Plane $$\pi_2$$ is defined as: $$y = 9510236 = 710184 + 4\cdot 2200013.$$

#3
In this example:


 * Plane $$\pi_1$$ is same as plane $$\pi_1$$ above.


 * Plane $$\pi_2$$ is defined as: $$z = -3464314.$$

Solve simultaneous linear congruences.
Given:

$$x \equiv 7 \pmod {19}\ \dots\ (1)$$

$$x \equiv 25 \pmod {31}\ \dots\ (2)$$

Calculate all values of $$x$$ that satisfy both congruences $$(1), (2).$$

From $$(1):\ x = 7 + 19\cdot p\ \dots\ (3)$$

From $$(2):\ x = 25 + 31\cdot q\ \dots\ (4)$$

Combine $$(3), (4):$$

$$7 + 19\cdot p\ = 25 + 31\cdot q$$

$$19\cdot p\ + 7 - 25 = 31\cdot q$$

$$19\cdot p\ - 18 = 31\cdot q$$

$$19\cdot p\ \equiv 18 \pmod {31}$$

$$p\ \equiv 14 \pmod {31}$$

$$p = 14 + 31\cdot r$$

Substitute this value of $$p$$ into $$(3):$$

$$x = 7 + 19\cdot (14 + 31\cdot r)$$

$$x = 7 + 19\cdot14 + 19\cdot 31\cdot r$$

$$x = 273 + 589\cdot r$$ where $$r$$ is type integer.

Check:

$$(273 + 589\cdot r)\ %\ 19 = 7 + 0 = 7.$$

$$(273 + 589\cdot r)\ %\ 31 = 25 + 0 = 25.$$

=Quadratic Congruences=

Introduction
A quadratic congruence is a congruence that contains at least one exact square, for example:

$$x^2 \equiv y \pmod{N}$$ or $$x^2 \equiv y^2 \pmod{N}.$$

Initially, let us consider the congruence: $$x^2 \equiv y \pmod{N}.$$

If $$y = x^2 - N,$$ then:

$$x^2 \equiv y \pmod{N}.$$

Proof: $$x^2 - y = x^2 - (x^2 - N) = N$$ which is exactly divisible by $$N.$$

Consider an example with real numbers.

Let $$N = 257$$ and $$26 \ge x \ge 6.$$ A cursory glance at the values of $$x^2 - N$$ indicates that the value $$x^2 - N$$ is never divisible by $$5.$$

Proof: $$N \equiv 2 \pmod{5}$$ therefore $$N - 2 = k5$$ or $$N = 5k + 2.$$

The table shows all possible values of $$x\ %\ 5$$ and $$y\ %\ 5:$$

As you can see, the value $$y = x^2 - N$$ is never exactly divisible by $$5.$$

If you look closely, you will see also that it is never exactly divisible by $$3$$ or $$7.$$

However, you can see at least one value of $$y$$ exactly divisible by $$11$$ and at least one value of $$y$$ exactly divisible by $$13.$$

The table shows all possible values of $$x\ %\ 11$$ and $$y\ %\ 11:$$

The two lines marked by an $$*$$ show values of $$y$$ exactly divisible by $$11.$$ The two values of $$x,$$ $$11p+2$$ and $$11p+9,$$ or $$11p \pm 2$$ are solutions of the congruence.

Why are values of $$y$$ divisible by some primes and not divisible by other primes? An interesting question that leads us to the topic of quadratic residues.

Quadratic Residues
Consider all the congruences for prime number $$5:$$

$$x^2 \equiv y \pmod{5}$$ for $$5 > x \ge 0.$$

Quadratic residues of $$5$$ are $$0,1,4.$$

Values $$2,3$$ are not quadratic residues of $$5.$$ These values are quadratic non-residues.

To calculate the quadratic residues of a small prime $$p:$$ Quadratic residues of $$11$$ are $$0,1,3,4,5,9.$$

The method presented here answers the question, "What are the quadratic residues of p?"

If $$p$$ is a very large prime, the question is often, "Is r a quadratic residue of p?" The answer is found in advanced number theory.

Let us return to quadratic residues mod $$N = 257.$$

$$N\ %\ 5 = 2,$$ therefore $$N$$ is not a quadratic residue of $$5.$$ This is why $$x^2 - N$$ is never divisible by $$5$$ exactly.

$$N\ %\ 11 = 4,$$ therefore $$N$$ is a quadratic residue of $$11$$ and a value of $$x$$ that satisfies the congruence $$x^2 \equiv 4 \pmod{257}$$ has form $$11p \pm 2.$$

From the table above:

These $$4$$ values of $$x^2 - N$$ are exactly divisible by $$11.$$

$$x = 9$$ is $$11\cdot 1 - 2.$$

$$x = 13$$ is $$11\cdot 1 + 2.$$

$$x = 20$$ is $$11\cdot 2 - 2.$$

$$x = 24$$ is $$11\cdot 2 + 2.$$

Products
This section uses prime number $$41$$ as an example.

Using  quadratic residues of $$41$$ are: Quadratic non-residues of $$41$$ are:

of 2 residues
A simple test to verify that the product of 2 residues is a residue: This test shows that, at least for prime number $$41,$$ the product of 2 residues is a residue. Advanced math proves that this is true for all primes.

of 2 non-residues
A simple test to verify that the product of 2 non-residues is a residue: This test shows that, at least for prime number $$41,$$ the product of 2 non-residues is a residue. Advanced math proves that this is true for all primes.

of residue and non-residue
A simple test to verify that the product of residue and non-residue is non-residue: This test shows that, at least for prime number $$41,$$ the product of residue and non-residue is non-residue. Advanced math proves that this is true for all primes.

Some authors may consider $$0$$ as not a legitimate residue.

$$0$$ is not included as a residue in the test above.

Euler's criterion
In number theory, Euler's criterion is a formula for determining whether or not an integer is a quadratic residue modulo a prime number. Precisely,

Let p be an odd prime and a be an integer coprime to p. Then



a^{\tfrac{p-1}{2}} \equiv \begin{cases} \;\;\,1\pmod{p}& \text{ if there is an integer }x \text{ such that }a\equiv x^2 \pmod{p},\\ -1\pmod{p}& \text{ if there is no such integer.} \end{cases} $$

Euler's criterion can be concisely reformulated using the Legendre symbol:

\left(\frac{a}{p}\right) \equiv a^{\tfrac{p-1}{2}} \pmod p. $$


 * $$\left(\frac{a}{p}\right) =

\begin{cases} 1 & \text{if } a \text{ is a quadratic residue modulo } p \text{ and } a \not\equiv 0\pmod p, \\ -1 & \text{if } a \text{ is a non-quadratic residue modulo } p, \\ 0 & \text{if } a \equiv 0 \pmod p. \end{cases}$$

It is known that $$3$$ is a quadratic residue modulo $$11.$$

Therefore $$(3^5)\ %\ 11$$ should be $$1.$$

It is known that $$7$$ is a quadratic non-residue modulo $$11.$$

Therefore $$(7^5)\ %\ 11$$ should be $$-1.$$
 * $$10 \equiv -1 \pmod{11}$$

Python's decimal module provides a method for computing $$(a^x)\ %\ p$$ very efficiently for both small and very large numbers.
 * $$761838257286 \equiv -1 \pmod{761838257287}$$

Value $$a = 3456789$$ is not a quadratic residue modulo $$p = 761838257287.$$

An exact square such as $$1,4,9,16,25,\dots$$ is always a quadratic residue modulo an odd prime $$p.$$

Product of 2 residues
Let $$a,b$$ be quadratic residues modulo odd prime $$p.$$

Let $$q = \frac{p-1}{2}.$$

Then:

$$a^q \equiv 1 \pmod p$$

$$b^q \equiv 1 \pmod p$$

By law of multiplication:

$$(a^q)(b^q) \equiv (1)(1) \pmod p$$ or

$$(a\cdot b)^q \equiv 1 \pmod p$$

Product $$(a\cdot b)$$ of 2 quadratic residues $$a, b$$ is quadratic residue.

Similarly, product of 2 non-residues is residue, and product of residue and non-residue is non-residue.

Factors of integer N
Several modern methods for determining the factors of a given integer attempt to create two congruent squares modulo integer $$N.$$

$$x^2 \equiv y^2 \pmod{N}$$

This means that the difference between the two squares is exactly divisible by $$N$$: $$N\mid (x^2 - y^2).$$

Integer $$N$$ always contains the factors $$N,1,$$ called trivial factors.

If $$N$$ contains two non-trivial factors $$p,q,$$ then:

$$\frac{(x+y)(x-y)}{p \cdot q}.$$

With a little luck $$p\mid (x+y)$$ and $$q\mid (x-y)$$ in which case:

$$p = \text{igcd}(x+y, N)$$ and $$q = \text{igcd}(x-y, N)$$ where

"$$\text{igcd}$$" is function "$$\text{integer greatest common divisor.}$$"

A simple example:
We will use quadratic congruences to calculate factors of $$N = 4171$$ for $$164 \ge x \ge 1.$$

Right hand side exact square
One congruence produced an exact square for y:
 * $$4900 \equiv 729 \pmod{N}$$
 * $$70^2 \equiv 27^2 \pmod{N}$$

$$p = \text{igcd}(70-27, 4171)$$ $$= \text{igcd}(43, 4171)$$ $$= 43.$$

$$q = \text{igcd}(70+27, 4171)$$ $$= \text{igcd}(97, 4171)$$ $$= 97.$$

Non-trivial factors of $$4171$$ are $$43,97.$$

Right hand side negative
Table below contains a sample of values of $$x$$ that produce negative $$y:$$

Non-trivial result 1
The congruences:


 * $$64 \equiv -4107 \pmod{N}$$
 * $$4096 \equiv -75 \pmod{N}$$
 * $$64\cdot 4096 \equiv -4107\cdot (-75) \pmod{N}$$
 * $$262144 \equiv 308025 \pmod{N}$$
 * $$512^2 \equiv 555^2 \pmod{4171}$$

$$p = \text{igcd}(555-512, 4171)$$ $$= \text{igcd}(43, 4171)$$ $$= 43.$$

$$q = \text{igcd}(555+512, 4171)$$ $$= \text{igcd}(1067, 4171)$$ $$= 97.$$

Non-trivial factors of $$4171$$ are $$43,97.$$

Non-trivial result 2
The congruences:
 * $$121 \equiv -4050 \pmod{N}$$
 * $$3721 \equiv -450 \pmod{N}$$
 * $$121\cdot 3721 \equiv -4050 \cdot (-450) \pmod{N}$$


 * $$450241 \equiv 1822500 \pmod{N}$$
 * $$671^2 \equiv 1350^2 \pmod{4171}$$

$$p = \text{igcd}(1350-671, 4171)$$ $$= \text{igcd}(679, 4171)$$ $$= 97.$$

$$q = \text{igcd}(1350+671, 4171)$$ $$= \text{igcd}(2021, 4171)$$ $$= 43.$$

Non-trivial factors of $$4171$$ are $$43,97.$$

Right hand side positive
Table below contains a sample of values of $$x$$ that produce positive $$y:$$

Non-trivial result
The congruences:


 * $$4225 \equiv 54 \pmod{N}$$
 * $$7921 \equiv 3750 \pmod{N}$$
 * $$4225\cdot 7921 \equiv 54 \cdot 3750 \pmod{N}$$


 * $$33466225 \equiv 202500 \pmod{N}$$
 * $$5785^2 \equiv 450^2 \pmod{4171}$$

$$p = \text{igcd}(5785-450, 4171)$$ $$= \text{igcd}(5335, 4171)$$ $$= 97.$$

$$q = \text{igcd}(5785+450, 4171)$$ $$= \text{igcd}(6235, 4171)$$ $$= 43.$$

Non-trivial factors of $$4171$$ are $$43,97.$$

Trivial result
The congruences:


 * $$7921 \equiv 3750 \pmod{N}$$
 * $$21025 \equiv 16854 \pmod{N}$$
 * $$7921\cdot 21025 \equiv 3750 \cdot 16854 \pmod{N}$$


 * $$166539025 \equiv 63202500 \pmod{N}$$
 * $$12905^2 \equiv 7950^2 \pmod{4171}$$

$$p = \text{igcd}(12905-7950, 4171)$$ $$= \text{igcd}(4955, 4171)$$ $$= 1.$$

$$q = \text{igcd}(12905+7950, 4171)$$ $$= \text{igcd}(20855, 4171)$$ $$= 4171.$$

This congruence produced the trivial factors of $$4171.$$

With 3 congruences
The congruences:


 * $$3136 \equiv -1035 \pmod{N}$$
 * $$3481 \equiv -690 \pmod{N}$$
 * $$21025 \equiv 16854 \pmod{N}$$
 * $$3136\cdot 3481 \cdot 21025 \equiv -1035 \cdot -690 \cdot 16854 \pmod{N}$$


 * $$229517646400 \equiv 12036284100 \pmod{N}$$
 * $$479080^2 \equiv 109710^2 \pmod{4171}$$

$$p=\text{igcd}(479080-109710,4171)$$ $$= 43.$$

$$q = \text{igcd}(479080+109710, 4171)$$ $$= 97.$$

Non-trivial factors of $$4171$$ are $$43,97.$$

=Links to related topics=

Quadratic Residue

Modular Arithmetic

Leonhard Euler, Euler's Criterion

Adrien-Marie Legendre, Legendre Symbol

Carl Friedrich Gauss, Triple_bar or $\equiv$, Quadratic reciprocity

Carl Pomerance, Quadratic sieve

Greatest common divisor, Greatest common divisor (Example of Recursion)

Python's decimal Module