TITLE: Efficiently finding two squares which sum to a prime
QUESTION [39 upvotes]: The web is littered with any number of pages (example) giving an existence and uniqueness proof that a pair of squares can be found summing to primes congruent to 1 mod 4 (and also that there are no such pairs for primes congruent to 3 mod 4).
However, none of the stuff I've read on the topic offers any help with actually efficiently finding (ie other than a straight search up to sqrt(p)) the concrete values of such squares.
What's the best way to actually find them ?

REPLY [12 votes]: I must add Gauss's construction, which I learned about from p.64 of Stark's "An Introduction to Number Theory".  If by "best", you mean computationally most efficient, then certainly the following is not the best way.  But there are other ways to measure quality...
Quoting Stark:
"In 1825 Gauss gave the following construction for writing a prime congruent to $1 \pmod{4}$ as a sum of two squares:  Let $p=4k+1$ be a prime number.  Determine $x$ (this is uniquely possible...) so that
$$ x = \frac{(2k)!}{2(k!)^2} \pmod{p}, \quad |x| < \frac{p}{2}$$
Now determine $y$ so that
$$ y = x  \cdot (2k)! \pmod{p}, \quad |y| < \frac{p}{2}$$
Gauss showed that $x^2+y^2=p$."