TITLE: Testing whether an integer is the sum of two squares
QUESTION [46 upvotes]: Is there a fast (probabilistic or deterministic) algorithm for determining whether an integer $n$ is a sum of two squares?
By "fast" here I mean polynomial time (i.e. time $O((\log n)^{O(1)})$). Note that I am interested only in whether the integer can be represented in such a way, not in how it is represented.
Since a fast algorithm is required, it will not do to use factorization. 
It would be odd if this turned out to be harder than detecting primality, since prime numbers are rarer. 
(This is a question that came up in a talk I just gave.)

REPLY [12 votes]: There are situations where we know that a number is a sum of two squares although we do not know the factorization, and in fact know very little about the factors. This question, for example, notes that $F_{2k+1}=F_k^2+F_{k+1}^2$ (the $F_k$ are the Fibonacci numbers). On the flip side, though, while Fibonacci numbers are easier to factor than most, they are not trivial (I think Blair Kelly maintains a list of factors).
To be specific, $F_{1049}$ is a sum of two squares, but has not yet been factored.