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!
 A: $\mathbb Z[\sqrt{n}]$ is integrally closed in $\mathbb Q(\sqrt{n})$ ($n\in\mathbb Z$, $n\neq1$) if and only if $n$ is square free and $n$ is not congruent to $1$ mod $4$ (or $n$ is a perfect square, in that case we have $\mathbb Z$ and $\mathbb Q$; thanks Arturo). 
Moreover, if $n\equiv1 \pmod 4$, then $\mathbb Z[\frac{1+\sqrt{n}}{2}]$ is integrally closed in $\mathbb Q(\sqrt{n})$. 
Sketch of proof of why $\mathbb Z[\sqrt{n}]$ is integrally closed in $\mathbb Q(\sqrt{n})$ for $n$ a square free number not congruent to $1$ modulo $4$. Let $\mathcal O$ be the set of integers numbers in $\mathbb Q(\sqrt{n})$. We can see that $\mathbb Z[\sqrt{n}]\subseteq\mathcal O$ (looking for suitable polynomials).
Let $\alpha=p+q\sqrt{n}\in\mathcal O-\mathbb Z$ with $p,q\in\mathbb Q$. $\alpha$ is a root of the polynomial
$$
f(x)=(x-\alpha)(x-\bar\alpha)=x^2-2px+(p^2-nq^2).
$$
But $f(x)$ is monic and of minimal degree (with coefficients in $\mathbb Q$), so it has to divide to the monic polynomial $g(x)\in\mathbb Z[x]$ which $g(\alpha)=0$. This implies that $f(x)\in\mathbb Z[x]$, thus $2p,\; p^2-mq^2\in\mathbb Z$. Now, you should prove that $p$ and $q$ are in $\mathbb Z$ since $n\equiv 2,3\pmod4$.
A: 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$.
A: Let's assume that $n$ is squarefree. I think it would be best to explore this without using knowledge of the answer. You want to find the integers in $\mathbf Q(\sqrt n)$. These are elements which satisfy a quadratic with integer coefficients. Every element looks like $a + b\sqrt{n}$ for some $a, b \in \mathbf Q$.
Recall that the conjugate of this element will be $a - b\sqrt n$. So it certainly satisfies
\[
(X - a - b\sqrt n)(X - a + b\sqrt n) = X^2 - 2aX + a^2 - nb^2.
\]
You need the coefficients $-2a$ and $a^2 - nb^2$ to lie in $\mathbf Z$. (You might recognize these as the trace and norm.) If this is the case, then as $(2a)^2 - n(2b)^2$ is an integer (divisible by $4$) and $n$ is squarefree, it follows that $2b \in \mathbf Z$ as well.
At this point, it makes sense to work mod $4$: we know that this last expression vanishes there, and the only squares are $0$ and $1$. For example, if $n \equiv 2$ then $(2a)^2$ and $(2b)^2$ must both be congruent to $0$, and it follows that $a$ and $b$ are in $\mathbf Z$.
