TITLE: Number of integer solutions of $x^2 + y^2 = k$
QUESTION [20 upvotes]: I'm looking for some help disproving an answer provided on a StackOverflow question I posted about computing the number of double square combinations for a given integer.  
The original question is from the Facebook Hacker Cup  

Source: Facebook Hacker Cup
  Qualification Round 2011
A double-square number is an integer $X$
  which can be expressed as the sum of
  two perfect squares. For example, 10
  is a double-square because $10 = 3^2 + 1^2$.
  Given $X$, how can we determine the
  number of ways in which it can be
  written as the sum of two squares? For
  example, $10$ can only be written as $3^2 + 1^2$
  (we don't count $1^2 + 3^2$ as being different). On the other hand,
  $25$ can be written as $5^2 + 0^2$ or as
  $4^2 + 3^2$.
You need to solve this problem for $0 \leq X \leq 2,147,483,647$.
Examples:
$10 \Rightarrow 1$
$25 \Rightarrow 2$
$3 \Rightarrow 0$
$0 \Rightarrow 1$
$1 \Rightarrow 1$

In response to my original question about optimizing this for F#, I got the following response which I'm unable to confirm solves the given problem correctly.  

Source: StackOverflow answer by Alexandre C.
Again, the number of integer solutions
  of $x^2 + y^2 = k$ is four times the
  number of prime divisors of $k$ which
  are equal to $1 \bmod 4$.
Knowing this, writing a program which
  gives the number of solutions is easy:
  compute prime numbers up to $46341$ once
  and for all.
Given $k$, compute the prime divisors of
  $k$ by using the above list (test up to
  $\sqrt{k}$). Count the ones which are
  equal to $1 \bmod 4$, and sum. Multiply
  answer by $4$.

When I go through this algorithm for $25$, I get $8$ which is not correct.  


For each prime factor (pf) of $25$ ($5$, $5$ are prime factors of $25$)
  
  
if pf % $4$ = $1$ (true for both $5$'s), add $1$ to count

return $4$ * count (count would be $2$ here). 

So for $25$, this would be $8$

REPLY [3 votes]: A simpler way to look at this would be to consider the factorization of $\displaystyle n$ in Gaussian Integers: $\displaystyle \mathbb{Z}[i] = \{a + bi \ \ | \ a \in \mathbb{Z}, \ b \in \mathbb{Z}\}$.
It is well known that that

Gaussian integers have unique factorization property (upto units $\displaystyle \pm 1, \pm i$).
Primes (of $\displaystyle \mathbb{Z}$) of the form $\displaystyle 4k+3$ are also primes in $\displaystyle \mathbb{Z}[i]$.
Primes (of $\displaystyle \mathbb{Z}$) of the form $\displaystyle 4k+1$ factorize into $\displaystyle w w'$ for a prime $\displaystyle w$ (in $\displaystyle \mathbb{Z}[i]$) and $\displaystyle w'$ is the conjugate of $\displaystyle w$. In fact, if $\displaystyle w = a+ib$, then the prime is of the form $\displaystyle a^2 + b^2$.
$\displaystyle 2 = (1+i)(1-i)$ and $\displaystyle 1+i$ is prime.

Now if $\displaystyle n = x^2 + y^2$ then $\displaystyle n = (x+iy)(x-iy)$, which corresponds to factorization of $\displaystyle n$ in $\displaystyle \mathbb{Z}[i]$, which you can get from the prime factorization of $\displaystyle n$ in $\mathbb{Z}[i]$ by choosing a subset of primes to multiply, to get $\displaystyle x + iy$. The conjugates of those primes go toward $\displaystyle x-iy$. The remaining need to be a perfect square to distribute once each into $\displaystyle x+iy$ and $\displaystyle x-iy$. Note this implies that primes of the form $\displaystyle 4k+3$ (which are also primes in $\displaystyle \mathbb{Z}[i]$) need to have an even power in the factorization.
So it basically amounts to finding out the different factorizations of the perfect squares which are factors of $\displaystyle n$ (after ignoring primes of the form $\displaystyle 4k+3$).
Note: It is the units which gives the multiple of $4$, if you count $(x,y), (y,x), (-x,y),(y,-x)$.
(Of course, this is not a formal proof...)
Hope the helps.