Three. (20 points) Given the sequence $\left\{a_{n}\right\}$ with the general term $a_{n}=$ $n$, and the sum of the first $n$ terms is $S_{n}$. If $S_{n}$ is a perfect square, find $n$.


Three, according to the problem, we have $S_{n}=\frac{n(n+1)}{2}=y^{2}$, which means $(2 n+1)^{2}-8 y^{2}=1\left(n 、 y \in \mathbf{N}_{+}\right)$.
Thus, the problem is transformed into:
Finding the positive integer solutions of the Pell equation $x^{2}-8 y^{2}=1$.
Obviously, $(3,1)$ is a minimal positive integer solution of the above equation.
Therefore, all solutions are given by $x+\sqrt{8} y=(3+\sqrt{8})^{m}$, where $m \in \mathbf{N}_{+}, x, y \in \mathbf{N}_{+}$.
Since $x+\sqrt{8} y=(3+\sqrt{8})^{m}$, then $x-\sqrt{8} y=(3-\sqrt{8})^{m}$.
Thus, $x=\frac{(3+\sqrt{8})^{m}+(3-\sqrt{8})^{m}}{2}$, which means
$$
2 n+1=\frac{(3+\sqrt{8})^{m}+(3-\sqrt{8})^{m}}{2} \text {. }
$$

Therefore, $n=\frac{(\sqrt{2}+1)^{2 m}+(\sqrt{2}-1)^{2 m}-2}{4}\left(m \in \mathbf{N}_{+}\right)$.