TITLE: Given $d \equiv 5 \pmod {10}$, prove $\mathcal{O}_{\mathbb{Q}(\sqrt{d})}$ never has unique factorization
QUESTION [14 upvotes]: With the exception of $d = 5$, which gives $\mathbb{Z}[\phi]$, of course (as was explained to me in another question). I'm not concerned about $d$ negative here, though that might provide a clue I have overlooked.
What I thought about was the fact that $2, 3, 7$ are irreducible in such a domain. But that does not necessarily guarantee that at least one of $6, 14, 21$ is going to have an alternate distinct factorization. I also have this vague inkling that $5$ being prime has something to do with it.

EDIT: Oops, sorry, I forgot to specify $d$ is squarefree. Thank you for forgiving this little mistake.

REPLY [8 votes]: The following proof uses ideas due to Ennola (Two elementary proofs 
concerning simple quadratic fields) and Redei (Über die 
quadratischen Zahlkörper mit Primzerlegung) and can be used to show 
that a necessary condition for  ${\mathbb Q}(\sqrt{m})$ to have unique factorization is that its discriminant is prime or a product of two negative prime discriminants. 
Assume that $K = {\mathbb Q}(\sqrt{5m})$ has unique factorization 
and consider the ideal $P = (5,\sqrt{m})$. Then $P^2 = (5)$. Since
$K$ has unique factorization, $P$ is principal, hence there exists 
$\pi \in {\mathcal O}_K$ with $P = (\pi)$. From $P^2 = (\pi^2) = (5)$
we deduce that there exists a unit $\eta \in {\mathcal O}_K$ with
$\pi^2 = 5\eta$.   
If $m < 0$, then $\eta = \pm 1$, hence $\sqrt{5} \in K$ or 
$\sqrt{-5} \in K$: contradiction.
If $m > 0$, then either $\eta = 1$ and $m = 1$, i.e., 
$K = {\mathbb Q}(\sqrt{5})$, or $\eta \ne 1$. In the second case
we have $m > 1$, and we may assume that $\eta$ is the fundamental
unit (squares of units may be subsumed into $\pi$). Then there 
is a prime $q \mid m$, and as above we find that $q\eta = \lambda^2$. 
This implies $\sqrt{5q} \in K$, hence $K$ is the product of two 
prime discriminants. In addition, $25N(\eta) = N(5\eta) = N(\pi^2) = 25$
implies that $N\eta = +1$, so the fundamental unit has norm $+1$.
Since $q \equiv 1 \bmod 4$ (discriminants are never $\equiv 3 \bmod 4$)
we can write $5q = a^2 + b^2$ with $a$ odd.  Setting $\alpha = b + \sqrt{5q}$
and $Q = (a,\alpha)$ we find from $\alpha \alpha' = -a^2$ that
$Q^2 = (a^2,2a\alpha,\alpha^2) = (\alpha\alpha',2a\alpha,\alpha^2) = 
  (\alpha)(\alpha',2a, \alpha) = (\alpha)$ since $\alpha$ and $\alpha'$
are coprime as ideals. Since $Q = (\omega)$ is principal, the element
$\omega^2/\alpha$ is a unit with negative norm: contradiction.
In the proof we have used that unique factorization holds if and only
if $h = 1$. If $P = (5,\sqrt{5m})$ is not principal, then 
$5 \cdot m = \sqrt{5m} \cdot \sqrt{5m}$ is an example of non-unique
factorization. If $P$ is principal (e.g. when $m = 41$), then
$-a^2 = (b+\sqrt{5m})(b-\sqrt{5m})$ should give rise to an example
of non-unique fasctorization, but I haven't checked the details.