TITLE: Distribution of moduli of quadratic residues
QUESTION [9 upvotes]: Let $D$ be a fixed positive squarefree integer. For a positive integer $x$, define
$S(D,x) = \{ q < x : D \text{is a quadratic residue} \pmod q \}$.
Here $q$ can be any integer, not necessarily a prime. Are elements of $S(D,x)$ evenly distributed? In other words,
let $0 < a < b < 1$ be constants and consider an interval $I = [ax,bx]$. Ideally I would like to see some result
that says that the number of elements of $S(D,x)$ in $I$ is proportional to the length of $I$ on the average as $x$ goes
to infinity (is this true?).
I am aware of classical 1917-1918 results of Vinogradov and Polya (and some later developments) about
distribution of quadratic residues, which in particular imply that quadratic residues modulo a fixed prime $p$ are
evenly distributed in the interval $[0,p]$ in the same sense as I described above. What I need, however, is a
result on the distribution of moduli with respect to which a fixed integer is a quadratic residue, and I cannot
find anything like this in the literature.
In other words, I am wondering how the divisors of $x^2-D$ are distributed on the average as $x$ goes to infinity.
It is a well-known fact that for an arbitrary integer $y$, there are unproportionally many (on the average) small
and large divisors of $y$ as $y$ goes to infinity, i.e., divisors are not uniformly distributed. But what if we take
$y$ in the special form $x^2-D$?
Any thoughts on the subject are much appreciated!

REPLY [8 votes]: For $D=-1$, Landau proved that
$$\# S(-1, x) \sim K \frac{x}{\sqrt{\log x}}$$
where $K = \frac{1}{\sqrt{2}} \prod_{p \equiv 3 \bmod 4} \frac{1}{\sqrt{1-p^{-2}}}$.
This shows that, for fixed $0<a<b$, 
$$\# S(-1,x) \cap [ax,bx] \sim K \left( \frac{bx}{\sqrt{\log(bx)}} -  \frac{ax}{\sqrt{\log(ax)}} \right)$$
and
$$ K \left( \frac{bx}{\sqrt{\log(bx)}} -  \frac{ax}{\sqrt{\log(ax)}} \right) = K \left( \frac{bx}{\sqrt{\log x + \log b}} -  \frac{ax}{\sqrt{\log x + \log a}} \right)$$
$$= K \left( \frac{bx}{\sqrt{\log x}} +O\left( \frac{x}{(\log x)^{3/2}} \right) - \frac{ax}{\sqrt{\log x}} - O\left( \frac{x}{(\log x)^{3/2}} \right) \right) \sim K \frac{(b-a)x}{\sqrt{\log x}}$$
So they are equidistributed in that $\frac{\#(S(-1,x) \cap [ax,bx])}{\#S(-1,x)}$ approaches $b-a$ but, if you ask for the denominator to be $x$, then the limit just goes to zero.
I believe the same should be true for any nonsquare $D$. Let 
$$Z_D(s) = \prod_{\left( \frac{D}{p} \right) = 1} \frac{1}{1-p^{-s}} = \sum_{D \ \mbox{is a square modulo}\ n} \frac{1}{n^s}.$$
The proof of Landau's theorem in Leveque's "Topics in number theory" comes down to analyzing the behavior of $Z_D(s)$ on $Re(s)=1$. The key facts are that $Z_{-1}(s) = C (s-1)^{1/2} + \cdots$ and $Z_{-1}(s)$ is otherwise bounded on $Re(s)=1$. These results hold for any nonsquare $D$. Let $\zeta_D(s)$ be the zeta function of $\mathbb{Q}(\sqrt{D})$. Then 
$$\frac{Z_D(s)^2}{\zeta_D(s)} =\left( \mbox{factors for primes dividing}\ 2D \right) \times \prod_{\left( \frac{D}{p} \right) = -1} \left( 1- \frac{1}{p^{2s}} \right) .$$
The right hand side is clearly convergent to the right of $Re(s)=1/2$, and $\zeta_D(s)$ is well known to have a simple pole at $s=1$ and no other poles on the line $Re(s)=1$.