What are the integers $n$ such that $\mathbb{Z}[\sqrt{n}]$ is integrally closed? I was recently reading about integral ring extensions. One of the first examples given is that $\mathbb{Z}$ is integrally closed in its quotient field $\mathbb{Q}$. Another is that $\mathbb{Z}[\sqrt{5}]$ is not integrally closed in $\mathbb{Q}(\sqrt{5})$ since for example $(1+\sqrt{5})/2\in\mathbb{Q}[\sqrt{5}]$ is integral over $\mathbb{Z}$ as a root of $X^2-X-1$, but $(1+\sqrt{5})/2\notin\mathbb{Z}[\sqrt{5}]$.

Now I'm  curious, can we find what are all integers $n$ such that $\mathbb{Z}[\sqrt{n}]$ is integrally closed (equal to its integral closure in its quotient field)?

One thing I do know is that unique factorization domains are integrally closed, so I think rings like $\mathbb{Z}[\sqrt{-1}]$, $\mathbb{Z}[\sqrt{-2}]$, $\mathbb{Z}[\sqrt{2}]$ and $\mathbb{Z}[\sqrt{3}]$ are integrally closed, as they are Euclidean domains, and thus are UFDs. But can we say what all integers $n$ are such that $\mathbb{Z}[\sqrt{n}]$ is integrally closed? Thanks!

Here's an extremely low-level way of doing it, just using the definition and some easy integer computations.
We may write $n=d^2m$, where $d$ and $m$ are integers, and $m$ is square free. Then $\mathbb{Q}(\sqrt{n}) = \mathbb{Q}(\sqrt{m})$, and 
$$\mathbb{Z}[\sqrt{n}] = \{a + db\sqrt{m}\mid a,b\in\mathbb{Z}\}.$$
If $m=1$, then $\mathbb{Z}[\sqrt{n}]=\mathbb{Z}$, $\mathbb{Q}(\sqrt{n}) = \mathbb{Q}$, so $\mathbb{Z}[\sqrt{n}]$ is integrally closed in $\mathbb{Q}[\sqrt{n}]$.
Assume then that $m\neq 1$.
If $d^2\gt 1$, then $\sqrt{m}\in\mathbb{Q}(\sqrt{n})$, and satisfies $x^2-m\in\mathbb{Z}[x]$, but is not in $\mathbb{Z}[\sqrt{n}]$. So we may assume $d^2=1$; that is, $n$ is squarefree.
Now assume that $a=\frac{p}{q} + \frac{r}{s}\sqrt{n}\in\mathbb{Q}(\sqrt{n})$ ($n$ squarefree, $n\neq 1$), $\gcd(p,q)=\gcd(r,s)=1$, $q\gt 0$, $s\gt 0$, is integral over $\mathbb{Z}$. If $r=0$, then $a\in\mathbb{Q}$, so by virtue of being integral it lies in $\mathbb{Z}\subseteq\mathbb{Z}[\sqrt{n}]$.
If $r\neq 0$, then $a$ satisfies the polynomial
$$\left( x - \frac{p}{q}-\frac{r}{s}\sqrt{n}\right)\left(x - \frac{p}{q}+\frac{r}{s}\sqrt{n}\right)$$
that is,
$$x^2 - \frac{2p}{q}x + \frac{p^2}{q^2}-\frac{r^2n}{s^2}.$$
For $a$ to be integral, this must have integer coefficients. In particular, $q|2p$, and so either $q=1$ or $q=2$.
If $q=1$, then we must have $s^2|n$. Since $n$ is squarefree, this requires $s=1$. So if $q=1$, then the element must be in $\mathbb{Z}[\sqrt{n}]$ and we are fine.
Can $q=2$? If $q=2$, then we need
$$\frac{p^2}{4} - \frac{r^2n}{s^2} = \frac{p^2s^2 - 4r^2n}{4s^2}\in\mathbb{Z}.$$
Therefore, $4|p^2s^2$; since $q=2$, then $p$ is odd, so $4|s^2$, and so $s=2k$ is even. We have
$$\frac{4p^2k^2 - 4r^2n}{16k^2} = \frac{p^2k^2-r^2n}{4k^2}\in\mathbb{Z}.$$
Now, both $p$ and $r$ are odd, so $p^2\equiv r^2\equiv 1\pmod{4}$; since $4$ divides $p^2k^2-r^2n$, and $p^2k^2-r^2n\equiv k^2-n\equiv 0\pmod{4}$, then remembering that $n$ is square free (hence not congruent to $0$ modulo $4$) we conclude that $n\equiv 1\pmod{4}$.
So the only way in which $\mathbb{Z}[\sqrt{n}]$ with $n$ squarefree can fail to be integrally closed is if $n\equiv 1\pmod{4}$. And indeed, if $n\equiv 1 \pmod{4}$, then we can take $p=r=1$, $q=s=2$, and we get that $\frac{1}{2}+\frac{1}{2}\sqrt{n}$ is integral but not in $\mathbb{Z}[\sqrt{n}]$. 
In summary: if $n=d^2m$ with $m$ square free, then:


*

*If $m=1$, then $\mathbb{Z}[\sqrt{n}]=\mathbb{Z}$ is integrally closed in $\mathbb{Q}(\sqrt{n}) = \mathbb{Q}$.

*If $m\gt 1$ and $d^2\gt 1$, then $\mathbb{Z}[\sqrt{n}]$ is not integrally closed in $\mathbb{Q}(\sqrt{n})$, witnessed by $\sqrt{m}$.

*If $d^2=1$, and $n\not\equiv 1\pmod{4}$, then $\mathbb{Z}[\sqrt{n}]$ is integrally closed in $\mathbb{Q}(\sqrt{n})$.

*If $d^2=1$ and $n\equiv 1\pmod{4}$, then $\mathbb{Z}[\sqrt{n}]$ is not integrally closed in $\mathbb{Q}(\sqrt{n})$, witnessed by $\frac{1}{2}+\frac{1}{2}\sqrt{n}$.


So $\mathbb{Z}[\sqrt{n}]$ is integrally closed if and only if $n$ is a perfect square, or $n$ is square free, different from $1$, and not congruent to $1$ modulo $4$.
