TITLE: Cardinality of hypersphere over $\mathbb{F}_p$ is always divisible by $p$?
QUESTION [8 upvotes]: I recently read an interesting post here (the second answer to this) about proving quadratic reciprocity by considering the set
$$S_p^{n-1} = \{(x_1,\dots,x_n)\in\mathbb{F}_p^n : x_1^2+\dots +x_n^2 = 1\}$$
Which is just the equation for an $(n-1)-$sphere with entries from a finite field.
Then, consider
$$N_p(n):= |S_p^{n-1}|$$
which just gives how many possible solutions to $x_1^2+\dots +x_n^2 = 1$ there are in $\mathbb{F}_p$.
It turns out that
$$N_p(2) \equiv 1 \mod p \iff p \equiv 3 \mod 4$$
and
$$N_p(2) \equiv -1 \mod p \iff p \equiv 1 \mod 4$$
for all $p$, which is interesting.
After reading the proof in the link above, I see why this is the case.
What I do not understand is that for $n>2$, I wrote some code and it seems
$$N_p(n) \equiv 0 \mod p$$
for all $p$.
So my question is

Why is it the case that $p$ divides $N_p(n)$ for all $n>2$? What does having a certain amount of solutions on a hypersphere over $\mathbb{F}_p$ have to do with being divisible by $p$?

REPLY [9 votes]: Here's a method which is overkill, but quite fun. Since you tagged your question as 'algebraic geometry', it doesn't seem entirely uncalled for.
Let

$X_n=V(x_1^2+\cdots+x_n^2-1)\subseteq \mathbb{A}^n_{\mathbb{F}_p}$
$\overline{X}_n=V(x_1^2+\cdots+x_n^2-x_{n+1}^2)\subseteq \mathbb{P}^n_{\mathbb{F}_p}$.

Let us note that $\overline{X}_n-X_n$ agrees set theoretically with the closed subscheme

$Z_n=V(x_1^2+\cdots+x_n^2)\subseteq \mathbb{P}^{n-1}_{\mathbb{F}_p}$.

In particular, we see that
$$\# X_n(\mathbb{F}_p)=\# \overline{X}_n(\mathbb{F}_p)-\# Z_n(\mathbb{F}_p).$$
Thus, it suffices to show that
$$\# \overline{X}_n(\mathbb{F}_p)=\# Z_n(\mathbb{F}_p)=1\mod p.$$
Let us make the observation that $\overline{X}_n$ and $Z_n$ are degree $2$ hypersurfaces in $\mathbb{P}^n_{\mathbb{F}_p}$ and $\mathbb{P}^{n-1}_{\mathbb{F}_p}$ respectively. We then start with the following lemma.

Lemma: Let $k$ be any field and let $S\subseteq \mathbb{P}^m_k$ be a degree $2$ hypersuface. If $m\geqslant 2$ then $$H^i(S,\mathcal{O}_S)=\begin{cases}k & \mbox{if}\quad i=0\\ 0 & \mbox{if}\quad \text{otherwise.}\end{cases}$$

Proof: This is a simple calculation. We have the usual exact sequence
$$0\to \mathcal{I}\to \mathcal{O}\to i_\ast\mathcal{O}_S\to 0,$$
where $\mathcal{I}$ is the ideal sheaf of $S$, $\mathcal{O}$ is short for $\mathcal{O}_{\mathbb{P}^m_k}$, and $i\colon S\hookrightarrow \mathbb{P}^m_k$ is the inclusion. Since $S$ has degree $2$ we have that $\mathcal{I}\cong \mathcal{O}(-2)$. The long exact sequence in cohomology gives
$$0\to H^0(\mathcal{O}(-2))\to H^0(\mathcal{O})\to H^0(i_\ast\mathcal{O}_S)\to \cdots\to H^j(\mathcal{O}(-2))\to H^j(\mathcal{O})\to H^j(i_\ast\mathcal{O}_S)\to\cdots,$$
where for shorthand I'm writing $H^i(-):=H^i(\mathbb{P}^m_k,-)$. Now, the standard computations of cohomology of $\mathbb{P}^m_k$ then imply that since $m\geqslant 2$ that $H^j(\mathcal{O})=H^j(\mathcal{O}(-2))=0$ for all $j$, except that $H^0(\mathcal{O})=k$. From this one easily deduces the desired claim since $H^j(S,\mathcal{O}_S)\cong H^j(i_\ast\mathcal{O}_S)$. $\blacksquare$
Using this lemma, and Fulton's trace formula (see [Mustata, Theorem 1.1]) we easily deduce the following which, as previously commented, implies your claim.

Proposition: Let $S$ be a degree $2$ hypersurface in $\mathbb{P}^m_{\mathbb{F}_p}$. If $m\geqslant 2$ then
$$\# S(\mathbb{F}_p)=1\mod p.$$

Proof: Fulton's trace formula implies that
$$\# S(\mathbb{F}_p)=\sum_{i=0}^{\dim(S)}(-1)^i\mathrm{tr}\left(\mathrm{Frob}_p|H^i(S,\mathcal{O}_S)\right)\mod p.$$
By the Lemma we know that the only non-vanishing term on the right is $H^0(S,\mathcal{O}_S)$ which is $\mathbb{F}_p$. Since $\mathrm{Frob}_p$ is the identity on $\mathbb{F}_p$ we deduce that that the right-hand side is $1$, from whence the claim follows. $\blacksquare$

Using the same methods, you can easily deduce the following.

Theorem: Let $d\geqslant 1$ be an integer and $q$ a power of $p$. Then, for any polynomial
$f(x_1,\ldots,x_n)\in\mathbb{F}_q[x_1,\ldots,x_n]$ such that $n>d$,
and for any $c\in \mathbb{F}_q^\times$ one has that
$$\#\left\{(x_1,\ldots,x_n)\in\mathbb{F}_q^n:
 f(x_1,\ldots,x_n)=c\right\}=0\mod p.$$

While not often thought about geometrically, this is all a special case of the classical theorem of Chevalley--Warning:

Theorem (Chevalley--Warning): Let $q$ be a power of $p$, and $f_1,\ldots,f_r$ elements of $\mathbb{F}_q[x_1,\ldots,x_n]$ of total
degrees $d_1,\ldots,d_m$. If $n>d_1+\cdots+d_m$, then
$$\#\left\{(x_1,\ldots,x_n)\in
\mathbb{F}_q^n:f_j(x_1,\ldots,x_n)=0\text{ for
> }j=1,\ldots,r\right\}=0\mod p.$$

You can see [CGS, Theorem 1.1] for an elementary proof.
But, what's cool is that the Fulton trace formula can give results in different directions. For instance, we have the following trivial corollary to Fulton's trace formula (which we used a special case of above in the Proposition).

Corollary(to Fulton's trace formula): Let $q$ be a power of $p$. Suppose that $X$ is a (geometrically connected) proper variety
over $\mathbb{F}_q$ such that $H^i(X,\mathcal{O}_X)$ is $0$ for $i>0$.
Then,
$$\# X(\mathbb{F}_q)=1\mod p.$$

This, in some sense, is more interesting to me than the Chevalley--Warning theorem, even though the latter more directly answers your question, because it really heavily implies the beautiful realization that the geometry of equations affects their number of solutions. This realization being the lynchpin for many of the most deep results we currently know about numbers of solutions to equations over finite fields (e.g. the Weil conjectures).
References:
[CGS] http://alpha.math.uga.edu/~pete/Chevalley_Warning_on_the_Boundary.pdf
[Mustata] http://www.math.lsa.umich.edu/~mmustata/lecture6.pdf