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 quadratic form $x^2+ny^2$ via 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 square-free 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 square-free 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 square-free, 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$.


