TITLE: When are quadratic rings of integers unique factorization domains?
QUESTION [13 upvotes]: Let $D$ be a square free integer. Let $R_D$ be the integral closure of $\mathbb{Z}$ in the field $\mathbb{Q}(\sqrt{D})$. 
For some values $D$, the ring $R_D$ is a $UFD$, but not for all. For example, the Gaussian integers $R_{-1}$ are a $UFD$ whereas the ring $R_{-5}$ is not. There are several ways to show this, including computing the class number of $R_D$. However, all the proofs I've seen feel ad hoc and unintuitive.
According to the Stark-Heegner theorem, for $D<0$, the ring $R_D$ is a $UFD$ if and only if 
$$D \in \{-1,-2,-3,-7,-11,-19,-43,-67,-163\}.$$
Is there any intuitive reason why this should be a complete list? Ideally there would be a structural reason - coming up with a separate proof for each $D$ in the list is deeply unsatisfying to me.

REPLY [7 votes]: I do think there is an intuitive reason, and it has to do with the number 41. You know that $n^2 + n + 41$ is prime for all $0 \leq n < 40$? Of course it's composite for $n = 41$ (and less obviously for $n = 40$).
Notice that $$\left(\frac{1}{2} - \frac{\sqrt{-163}}{2}\right)\left(\frac{1}{2} + \frac{\sqrt{-163}}{2}\right) = 41,$$ $$\left(\frac{3}{2} - \frac{\sqrt{-163}}{2}\right)\left(\frac{3}{2} + \frac{\sqrt{-163}}{2}\right) = 43,$$ $$\left(\frac{5}{2} - \frac{\sqrt{-163}}{2}\right)\left(\frac{5}{2} + \frac{\sqrt{-163}}{2}\right) = 47,$$ and so on and so forth to $$\left(\frac{79}{2} - \frac{\sqrt{-163}}{2}\right)\left(\frac{79}{2} + \frac{\sqrt{-163}}{2}\right) = 1601.$$
What are the other numbers $d$ such that $n^2 + n + d$ gives primes for $0 \leq n < d$? 2, 3, 5, 11, 17, see OEIS A014556. And then $-4d + 1$ gives $-7, -11, -19, -43, -67$, and $-163$ corresponds to 41.
No higher number $d$ meets this requirement, and thus does not correspond to a Heegner number.
EDIT: Corrected per a comment.