Let $\sigma\_0(n)$ be the divisor counting function:
$$\sigma\_0(n) = \sum\_{d \vert n} 1.$$
I ran some numerical experiments that showed when $p$ is prime, the function $\sigma\_0(n)$ is equidistributed mod $p$. That is, for any residue class $a \mod p$,
$$\lim\_{X \to \infty} \dfrac{ \vert \{ n<X: \sigma\_0(n) \equiv a \mod p \} \vert }{X} = \dfrac{1}{p}.$$


Is this fact correct? If so, could anyone sketch a proof / provide a reference for a proof?



Is the divisor counting function equidistributed mod $p$?
$\newcommand{\Y}{\mathfrak{X}\_p(X)}$I haven't checked all the details on the application of Selberg–Delange below, so it's possible something I am saying is nonsense, but even after accounting for the correction noted by Noam Elkies in the [comments](https://mathoverflow.net/questions/444371/is-the-divisor-counting-function-equidistributed-mod-p#comment1147153_444371), it appears to be the case that $\sigma\_0(n)$ is *not*, in general, equidistributed among the non-zero congruence classes mod $p$ unless $2$ is a primitive root modulo $p$ (see [A001122](https://oeis.org/A001122) on OEIS). In fact, we have that
$$\lim\_{X \to \infty}\biggl(\frac{1}{\Y}\sum\_{n\leqslant X} 1\_{\sigma\_0(n) \equiv a \mod p}\biggr) = \frac{1}{p-1}\biggl(1+\frac{1}{\delta\_p}\sum\_{\substack{\chi \neq \chi\_0\\\chi(2) = 1}} \overline{\chi}(a)G\_{\chi}(1)\biggr),$$
where the sum runs over all nontrivial Dirichlet character mod $p$ with $\chi(2) = 1$,
$$\Y = \sum\_{n\leqslant X} 1\_{p \nmid \sigma\_0(n)}, \qquad \delta\_p = \lim\_{p \to \infty} \frac{\Y}{X},$$
are the counting function and density of the set of $n$ with $p \nmid \sigma\_0(n)$ and $G\_\chi$ is defined below. Note if $n$ is squarefree, then $\sigma\_0(n) = 2^{\omega(n)}$, where $\omega(n)$ is the prime-divisor counting function, and hence $p \nmid \sigma\_0(n)$ for $p > 2$. Since the squarefrees have positive density, this implies that $\Y \gg X$ and $\delta\_p > 0$.


When $2$ is a primitive root modulo $p$, the only character with $\chi(2) = 1$ is the trivial one, so this would imply equidistribution in that case. But, for example, if you take $p = 7$ or $p = 17$, and consult the table of values of Dirichlet characters mod $p$, then you'll find that the only characters which have $\chi(2) = 1$ are the trivial character and the quadratic character; from this it follows that $\sigma\_0(n)$ is biased towards being a quadratic residue over being a nonresidue. The number of characters that will occur in this sum for a given prime $p$ is $1$ less than [A001917](https://oeis.org/A001917) on OEIS.


Roughly speaking, this is because $\sigma\_0(p) = 2$, and hence the characters which have $\chi(2) = 1$ give a main term contribution. Here's a sketch:


Guided by Weyl's criterion for $(\mathbb{Z}/p\mathbb{Z})^\times$, suppose $(a,p)=1$, and note that orthogonality of Dirichlet characters reads
$$1\_{n \equiv a \mod p} = \frac{1}{p-1}\sum\_{\chi} \overline{\chi}(a) \chi(n),$$ where the sum is over all Dirichlet characters mod $p$. Thus,
$$\frac{1}{\Y}\sum\_{n\leqslant X} 1\_{\sigma\_0(n) \equiv a \mod p} = \frac{1}{p-1} + \frac{1}{p-1}\sum\_{\chi\neq \chi\_0} \overline{\chi}(a)\left( \frac{1}{\Y} \sum\_{n\leqslant X} \chi(\sigma\_0(n)) \right), \tag{$\star$}\label{star}$$
where we have separated the contribution of the trivial character. It thus suffices to study
$$M\_\chi(X) = \sum\_{n\leqslant X} \chi(\sigma\_0(n)), $$
which is a mean-value of multiplicative function. The characters which satisfy $M\_\chi(X) = o(X)$ do not contribute a main term to \eqref{star} since $\Y \gg X$, while the characters with $M\_\chi(X) \asymp X$ do.


Standard multiplicative number theory techniques apply here. To put this into effect, define the Dirichlet series
$$F\_\chi(s) = \sum\_{n\geqslant 1} \frac{\chi(\sigma\_0(n))}{n^s},$$
so that an application of Perron's formula gives that
$$\sum\_{n\leqslant X} \chi(\sigma\_0(n)) = \frac{1}{2\pi i} \int\_{2-i\infty}^{2+i\infty} F\_\chi(s) X^s \frac{ds}{s}.$$
Investigating the Euler product of $F\_\chi$, we find
$$F\_\chi(s) = \prod\_p \biggl(\sum\_{k=0}^\infty \frac{\chi(\sigma\_0(p^k))}{p^{ks}}\biggr) = \prod\_p \biggl(\sum\_{k=0}^\infty \frac{\chi(k+1)}{p^{ks}}\biggr) = \zeta(s)^{\chi(2)} G\_\chi(s),$$
where $G\_\chi(s)$ is convergent in $\Re(s) > 1/2$. An application of the Selberg-Delange method (see Chapter 5 of [Tenenbaum's "Introduction to analytic and probabilistic number theory"](https://mathscinet.ams.org/mathscinet-getitem?mr=3363366), in particular Theorem 5.2 with $z = \chi(2)$, $N = 1$ and $F = F\_\chi$) should then give that
$$ \sum\_{n\leqslant X} \chi(\sigma\_0(n)) = \frac{X (\log X)^{\chi(2) - 1}}{\Gamma(\chi(2))} \biggl(G\_\chi(1) + O\Big(\frac{1}{\log X}\Big)\biggr),$$
from which it is clear that if $\chi(2) \neq 1$, then the expression on the right is $o(X)$ while, if $\chi(2) = 1$, then we get something of size $\asymp X$. Putting this back into \eqref{star}, and concentrating on the main terms proves the claim.


