<s>[INST] The quadratic form $x^2 + ny^2$ via prime factors 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? 
 [/INST] Alright, the answer to the actual question asked is yes, as follows. I am taking a prime $p$ with $\gcd(p,n) = 1.$ Then I am demanding $u^2 + n v^2 = p.$ Next, I am taking some number, composite or prime, call it $Q,$ and demand about $pQ$ rather than $Q$ itself,
$$ pQ = r^2 + n s^2.    $$
First, we get
$$ \left( \frac{u}{v} \right)^2  \equiv \left( \frac{r}{s} \right)^2  \equiv -n \pmod p.  $$ Choose $\pm s$ so that
$$ \left( \frac{u}{v} \right)  \equiv \left( \frac{r}{s} \right)  \pmod p.   $$
So we have
$ u s \equiv v r \pmod p,$ or
$$ -us + vr \equiv 0 \pmod p.  $$
Next,
$$ p^2 Q = (ur + n v s)^2 + n (-us + v r)^2,   $$ and so
$$  Q = \left( \frac{ur + n v s}{p} \right)^2 + n \left(\frac{-us + v r}{p} \right)^2   $$ in integers.
Combine this once again with $p = u^2 + n v^2$ and you get back to $pQ = r^2 + n s^2$ as desired.
</s>