sage: def sum_of_two_squares_naive(n): ... for i in range(int(sqrt(n))): ... if is_square(n - i^2): ... return i, (Integer(n-i^2)).sqrt() ... return "%s is not a sum of two squares"%nWe next use our function in a couple of cases.
sage: sum_of_two_squares_naive(23) '23 is not a sum of two squares' sage: sum_of_two_squares_naive(389) (10, 17) sage: sum_of_two_squares_naive(2007) '2007 is not a sum of two squares' sage: sum_of_two_squares_naive(2008) '2008 is not a sum of two squares' sage: sum_of_two_squares_naive(2009) (28, 35) sage: 28^2 + 35^2 2009 sage: sum_of_two_squares_naive(2*3^4*5*7^2*13) (189, 693)
so and . Since is a field we may divide by in the equation to see that Thus the Legendre symbol equals . However, by Proposition 4.2.1,
so if and only if is even, which is to say .
with and
Because is odd, , so Lemma 5.6.4 implies that , a contradiction.
To prepare for our proof of , we reduce the problem to the case when is prime. Write where has no prime factors . It suffices to show that is a sum of two squares, since
Since and , either there exists an such that , or the continued fraction expansion of is finite and is larger than the denominator of the rational number , in which case we take and are done. In the first case,
so satisfies the conclusion of the lemma.
so Proposition 4.2.1 implies that is a square modulo ; i.e., there exists such that . Lemma 5.6.5, with and , implies that there are integers such that and
Letting , we have that
so
But , so
Thus .
sage: def sum_of_two_squares(p): ... p = Integer(p) ... assert p%4 == 1, "p must be 1 modulo 4" ... r = Mod(-1,p).sqrt().lift() ... v = continued_fraction(-r/p) ... n = floor(sqrt(p)) ... for x in v.convergents(): ... c = r*x.denominator() + p*x.numerator() ... if -n <= c and c <= n: ... return (abs(x.denominator()),abs(c))Next we use the algorithm to write the first -digit prime as a sum of two squares:
sage: p = next_prime(next_prime(10^10)) sage: sum_of_two_squares(p) (55913, 82908)The above calculation was essentially instantanoues. If instead we use the naive algorithm from before, it takes several seconds to write as a sum of two squares.
sage: sum_of_two_squares_naive(p) (55913, 82908)
William 2007-06-01