Elementary algebra shows that the product of two numbers in the form $x^2 + ny^2$ again has the same form, since if $p = (a^2 + nb^2)$ and $q = (c^2 + nd^2)$, $$pq = (a^2 + nb^2)(c^2 + nd^2) = (ac \pm nbd)^2 + n(ad \mp bc)^2$$ My question is: Assuming that a number $z$ can be factored into primes of the form $x^2 + ny^2$, does every representation of $z$ in this form arise from repeated applications of this formula to the prime factors?

The answer is yes. To see this, consider the ring $R=\mathbb{Z}[\sqrt{n}]$. If $z=p_1\dots p_k$ is the decomposition of $z$ into rational primes, then by assumption each $p_j$ decomposes in $R$ as $p_j=q_j\bar q_j$. We need to show that any decomposition $z=r\bar r$ in $R$ can be gotten as follows: for each $j$ let $r_j$ be either $q_j$ or $\bar q_j$, and then put $q=wr_1\dots r_k$, where $w$ is a unit in $R$. In $R$ the ideal $(z)$ decomposes into prime ideals as $(z)=(q_1)(\bar q_1)\dots (q_k)(\bar q_k)$, hence it suffices to show that in $R$ the ideal $(z)$ and its divisors decompose uniquely into prime ideals. If $n$ is squarefree and congruent to $1$ or $2$ mod $4$, then $R$ is the full ring of integers in $\mathbb{Q}(\sqrt{n})$, hence it is a Dedekind domain. So in this case we are done. If $n$ is squarefree and congruent to $3$ mod $4$, then $R$ is a quadratic order of conductor $2$ in $\mathbb{Q}(\sqrt{n})$, hence unique factorization holds in $R$ for ideals prime to $2$. Clearly, each $p_j$ above is odd, hence $(z)$ is coprime to $2$, and we are done. If $n$ is not squarefree, then $R$ is a quadratic order of some conductor $f\mid 2n$ in $\mathbb{Q}(\sqrt{n})$, hence unique factorization holds in $R$ for ideals prime to $f$. Clearly, each $p_j$ above is at least $n$, hence $(z)$ is coprime to $2n$, and we are done. For the quoted result on quadratic orders see Exercise 7.26 in Cox: Primes of the form $x^2+ny^2$. 


Why not. My answer at http://math.stackexchange.com/questions/229201/thequadraticformx2ny2viaprimefactors/229270#229270 

