Representing integers as the sum of two squares
In this post we present a recursive algorithm for finding all the possible representations, as a sum of two squares, for any given integer that can be expressed this way.
Almost 400 years ago, Pierre de Fermat stated that every odd prime of the form p=4k+1 can be expressed as the sum of two squares:
p=x2+y2
with integers x and y, if and only if
p≡1mod4
Later on, around the year 1747, Leonhard Euler was able to prove Fermat's statement correct.
# Overview
For example, if n=12345678910111213, we can represent it as a sum of two squares in 4 different ways:
12345678910111213=12510822+1111040672
12345678910111213=135083172+1102869182
12345678910111213=524188772+979690782
12345678910111213=649597382+901438372
If the prime factorization of a number is known, it can be verified if the number is expressible as a sum of two squares by checking each prime factor modulo 4. If a prime factor is congruent to 3mod4 , then its power must be even. Otherwise, the number cannot be represented as a sum of two squares.
In other words, a positive integer can be written as a sum of two squares, if and only if its prime decomposition contains no prime congruent to 3mod4 raised to an odd power.
For example, 2450=2⋅52⋅72. Of the primes occurring in this decomposition, only 7 is congruent to 3mod4. Its exponent in the decomposition, 2, is even. Therefore, it must be expressible as the sum of two squares. Indeed, 2450=72+492=352+352.
On the other hand, the prime decomposition of 3430 is 2⋅5⋅73. This time, the exponent of 7 is an odd number. Therefore, 3430 cannot be written as the sum of two squares.
Based on the identity:
a2⋅(b2+c2)=(ab)2+(ac)2
we can see that every square multiplied with a prime of the form 4k+1 can also be represented as a sum of two squares. This identity will be used later on in our algorithm to, recursively, produce all the solutions.
# Algorithm
Assuming that we are given a positive integer n that can be represented as a sum of two squares, the first step of the algorithm is breaking this number into two parts: n=a⋅b, where a is the product of its prime factors congruent to 1mod4, and b is the product of its prime factors congruent to 3mod4 and 2mod4, each prime having an even power.If n has the form 22k+1⋅z, where z is odd, then a is multiplied by 2 and b is multiplied by 22k.
At the end of this construction, we can see that b is a square. The next step is setting b to its square root:
b:=√b
If a=1, then n can be represented only as n=b2+02.
If a=2, then n can be represented only as n=b2+b2.
If a>2, we need to find all the solutions to the following quadratic congruence:
x2≡ -1moda
Taking square roots modulo a prime power can be done efficiently by using the Tonelli–Shanks algorithm (see Dickson's reference and Hensel's lemma).
But since a may not be a prime power, we need to decompose it into prime powers pekk and take the square root of pekk-1 modulo pekk, where for each prime power we get back two square roots.
Then, by applying the Cartesian product to the list of pairs of square roots, we get all the Cartesian combinations.
For each Cartesian combination, we use the Chinese Remainder Theorem to combine the partial congruences into a solution to x.
For example, if n=pe11⋅pe22, then all the solutions to x2≡ -1modn can be generated in the following way:
A=[√pe11-1modpe11pe11pe11-(√pe11-1modpe11)pe11]
B=[√pe22-1modpe22pe22pe22-(√pe22-1modpe22)pe22]
Taking the Cartesian product of A and B, we generate the following 4 combinations:
[A1,B1]
[A1,B2]
[A2,B1]
[A2,B2]
By applying the Chinese Remainder Theorem, from each Cartesian combination we get a solution to x.
In the next step, for each solution s to x, we use a modified version of Euclid's algorithm to compute a modified greatest common divisor of s and a that is the largest value that satisfies s2≤a, defined as:
gcd(s,q)=(s,q) for s2≤a
gcd(s,q)=gcd(q,smodq)
This gives us the following two values:
(s,q):=gcd(s,a)
From which we generate a new solution, as:
n=(b⋅s)2+(b⋅(qmods))2
The last part of the algorithm is the recursive part, and it involves the identity described at the beginning, which uses each square p2k in a, to recursively represent ap2k as sums of two squares, then multiplying each solution with b⋅pk.
Algorithmically speaking:
Let pekk be the prime powers of a.
Let j:=ekmod2. When ek is even, j=0, otherwise j=1.
If j<ek, recursively find solutions to apek-jk.
Then multiply each solution by b⋅ptk, where t=ek-j2.
As long as j<ek, we increment j by 2 and repeat. Otherwise, we go to the next prime power of a and do the same steps.
In the end, all the non-negative integer solutions to n=x2+y2 are found.
# Implementations
Bellow we have three efficient implementations of the algorithm described in this post, which finds all the non-negative integer solutions to the equation n=x2+y2 for any given number n that can be represented as the sum of two squares, assuming that its prime factorization can be computed.Sum of two squares - algorithm implementations:
- Algorithm implemented in Perl
- Algorithm implemented in Sidef
- Algorithm implemented in Sidef + Tonelli-Shanks algorithm
Extra:
- Solutions to x = -1 (mod n)
- Solutions to x = 1 (mod n)
- Solutions to x = a (mod n)
- Primitive sum of two squares
- Sum of two squares - one solution
- Square root modulo n: Tonelli-Shanks algorithm
- Sum of an even number of positive squares - one solution
- Number of representations as sum of two squares
- Number of representations as sum of four squares
Comments
Post a Comment