How to compute the series $\sum_{n=0}^\infty q^{n^2}$?

Let $q\in (0,1)$. Is there a way of computing the series $$ \sum_{n=0}^\infty q^{n^2} $$ explicitly? Is there at least a nice accurate estimate?

All I could get is the estimate $$\sqrt{\frac{\pi}{4\cdot\mathrm{ln}\frac{1}{q}}}\leq\sum_{n=0}^\infty q^{n^2}\leq 1+\sqrt{\frac{\pi}{4\cdot\mathrm{ln}\frac{1}{q}}}$$ via integration (quite possibly flawed).

For $q=\frac{1}{2}$, Maple gives the values $$ 1.064467020\leq 1.564468414\leq 2.064467020, $$ showing that my estimate is not very precise. (Of course, the sum of the two errors will always be $1$. Here both errors are coincidentally almost exactly $\frac{1}{2}$.)


Solution 1:

As Qiaochu says, the Euler-Maclaurin formula is useful here. It gives the error in the approximation

$$\sum_{n=0}^{\infty} q^{n^2} \approx \sqrt{\frac{\pi}{4 \log (1/q)}} + \frac{1}{2}$$

to be exactly

$$ \lim_{a \to \infty} \int_0^a 2xq^{x^2} (\log q) \left(x - \lfloor x \rfloor - \frac{1}{2}\right) dx.$$

Since $q^{x^2} \to 0$ quite rapidly, the value of this integral can be approximated closely by using small values of $a$.

For example, if $q = 1/2$, the integral (i.e., the error in the approximation) evaluates to $1.39417 \times 10^{-6}$ (via Mathematica).

Solution 2:

$\theta_3(q)=\sum_{n=-\infty}^\infty q^{n^2}$ where $q=e^{-\pi s}$ and since, for $Re(s)>0,$

$$\sqrt{s} \theta_ 3(e^{-\pi s}) = \theta_3(e^{-\pi/s})$$

we have

$$\sum_{n=0}^\infty q^{n^2} = \sqrt{ \frac{\pi} {4 \log \left( \frac{1}{q} \right) } } \theta_3(e^{-\pi/s}) + \frac12.$$

But if $s$ is small then $e^{-\pi/s}$ is large and so $\theta_3(e^{-\pi/s}) \approx 1,$ and hence you obtain the result you noticed:

$$\sum_{n=0}^\infty q^{n^2} \approx \sqrt{ \frac{\pi} {4 \log \left( \frac{1}{q} \right) } } + \frac12.$$

For greater accuracy take more terms in the sum defining $\theta_3(e^{-\pi/s}).$

Solution 3:

The comment above, pointing out that the partial sums converge quickly, suggests you can improve your estimate nicely by using a bound for $R_n$, the remainder after the nth partial sum, given by the integral of $q^{x^2}$: $$ R_n = \int_n^\infty q^{x^2}\, dx = \sqrt{ \frac{\pi}{2\ln(1/q)}} ( 1- \mathrm{erf}( n \sqrt{\ln(1/q)})) $$

Even taking only $n=3$, for example, gives (for $q=1/2$) $$ 0.56401 < \sum_{n=1}^{\infty} q^{x^2} < 0.56489 $$