Representing integers as the difference of two squares
Most integers can be represented as a difference of two squares, where each square is a non-negative integer.
Algebraically speaking:
n=x2-y2
for some non-negative integers x and y.
In this blog post we're going to look at a simple and elegant algorithm for finding all the possible representations for any given integer that can be represented as a difference of two squares, using its divisors.
For example, if we have n=12345, we can represent it in four different ways as a difference of two non-negative integer squares:
12345=4192-4042
12345=12372-12322
12345=20592-20562
12345=61732-61722
a⋅b=(a+b2)2-(a-b2)2
Then, by iterating over all the divisors of n, we generate all the possible representations for n as a difference of two non-negative squares.
In order to generate only integer solutions, we have to make sure that a+b is even. In general, when n is even, we can iterate only over its even divisors. Otherwise, we iterate over its odd divisors.
For each such divisor d, we generate a new solution to n=x2-y2, as:
x=nd+d2
y=nd-d2
Illustrating this with an example, let n=24. As 24 is even, we are going to iterate only over its even divisors, which are: [2,4,6,8,12,24].
First divisor in the list, d=2:
x=nd+d2=7
y=nd-d2=5
Algebraically speaking:
n=x2-y2
for some non-negative integers x and y.
In this blog post we're going to look at a simple and elegant algorithm for finding all the possible representations for any given integer that can be represented as a difference of two squares, using its divisors.
For example, if we have n=12345, we can represent it in four different ways as a difference of two non-negative integer squares:
12345=4192-4042
12345=12372-12322
12345=20592-20562
12345=61732-61722
# Finding solutions
By writing n as the product of two whole numbers, n=a⋅b, we have the following identity:a⋅b=(a+b2)2-(a-b2)2
Then, by iterating over all the divisors of n, we generate all the possible representations for n as a difference of two non-negative squares.
In order to generate only integer solutions, we have to make sure that a+b is even. In general, when n is even, we can iterate only over its even divisors. Otherwise, we iterate over its odd divisors.
Algorithm:
Let d run over the divisors of n which are d≤√n and have the same parity as n.For each such divisor d, we generate a new solution to n=x2-y2, as:
x=nd+d2
y=nd-d2
Illustrating this with an example, let n=24. As 24 is even, we are going to iterate only over its even divisors, which are: [2,4,6,8,12,24].
First divisor in the list, d=2:
x=nd+d2=7
y=nd-d2=5
Second divisor in the list, d=4:
x=nd+d2=5
y=nd-d2=1
Solutions:
24=72-52
24=52-12
In fact, this are all the representations for 24 as a difference of two non-negative integer squares, because in the next iteration, d=6, we would have nd=4, and since nd<d, this would generate negative solutions, which are essentially the same as the positive ones, so we can stop here.
# Extra
Finding a non-trivial representation for a semiprime n as a difference of two squares, is just as hard as factoring n, because:x2-y2=(x+y)⋅(x-y)
For example, if we have n=15709691830279 and we also know a non-trivial representation for it:
n=43847802-18752612
Then we can factorize it as:
n=(4384780+1875261)⋅(4384780-1875261)
From this results that if we know only the sum or the difference of the prime factors of a semiprime, we can find its prime factors in polynomial time.
Let t be the absolute difference between the two prime factors of a semiprime n (i.e.: t=|p-q|, for n=p⋅q):
x= √t2+4n2
y=t2
Alternatively, let s be the sum of its two prime factors (i.e.: s=p+q, for n=p⋅q):
x=s2
y=√s2-4n2
Then n can be factorized in both cases as:
n=(x+y)⋅(x-y)
# Implementations
Bellow we have a Perl implementation of the algorithm described in this post.
Difference of two squares - algorithm implementation:
Extra:Next time we're going to look at an algorithm for representing an integer as a sum of two squares, which is a slightly more advanced problem.
See also:
Comments
Post a Comment