4. A farmer has a flock of $n$ sheep, where $2000 \leq n \leq 2100$. The farmer puts some number of the sheep into one barn and the rest of the sheep into a second barn. The farmer realizes that if she were to select two different sheep at random from her flock, the probability that they are in different barns is exactly $\frac{1}{2}$. Determine the value of $n$.
Solution 1: Suppose the farmer has $n$ sheep total and puts $k$ sheep into one of the barns. The probability that the farmer selects sheep from different barns is $\frac{k(n-k)}{\binom{n}{2}}$.
$$
\begin{aligned}
\frac{k(n-k)}{\binom{n}{2}} & =\frac{1}{2} \\
2 k(n-k) & =n(n-1) / 2 \\
(4 k) n-4 k^{2} & =n^{2}-n \\
n^{2}-(4 k+1) n+4 k^{2} & =0 \\
n & =\frac{(4 k+1) \pm \sqrt{(4 k+1)^{2}-16 k^{2}}}{2} \\
n & =\frac{(4 k+1) \pm \sqrt{8 k+1}}{2}
\end{aligned}
$$

In order for $n$ to be an integer, it is necessary to have $8 k+1=a^{2}$ for some positive integer $a$, so we must have $k=\frac{a^{2}-1}{8}$. Substituting this back into our expression for $n$ gives:
$$
\begin{aligned}
n & =\frac{4 a^{2}-4+8 \pm 8 a}{16} \\
& =\frac{4 a^{2}+4 \pm 8 a}{16} \\
& =\frac{a^{2} \pm 2 a+1}{4} \\
& =\left(\frac{a \pm 1}{2}\right)^{2}
\end{aligned}
$$

Thus, $n$ must be a perfect square. The only perfect square between 2000 and 2100 is 2025 , and when $a=89$ we get $n=2025, k=990$ as a solution. Thus, $n=2025$.
Solution 2: We proceed as in solution 1, getting $n^{2}-(4 k+1) n+4 k^{2}=0$ which we can rewrite as $n=n^{2}-4 k+4 k^{2}$. This simplifies to $n=(n-2 k)^{2}$, and so $n$ must be a perfect square, meaning $n=2025$.