# A fast Harmonic number Calculator

Up a level : The Harmonic series On this page you will find a harmonic number calculator. It will also calculate the inverse harmonic numbers. I suggest you run this in Chrome or Edge. Firefox is about 10 times slower. As a ballpark estimation of time, it takes my (rather old) laptop about 2 s to find the number of terms to get to the sum 10000 using Chrome (ver 97.0.4692.71), but about 20 s on Firefox (ver 95.0.2). It takes just shy of 1 s on chrome on my Samsung Note 9.

The calculations are done using between 1000 to 10000 decimals, or about 1000 to 20000 digits, dependent on the inputs.

The calculator works fine up to sums about 23000 because beyond that the denominator will be less than 10-10000, that will be rounded to 0.

To find the harmonic numbers it will use the so called DeTemple–Wang approximation. I found it here,
https://www.emis.de/journals/JIPAM/images/245_07_JIPAM/245_07.pdf
in a paper by Mark B. Villarino.

We have that ${H_n} \approx \ln (n + {\textstyle{1 \over 2}}) + \gamma + \frac{{10}}{{240{{\left( {n + {\textstyle{1 \over 2}}} \right)}^2}+42}}$

where lambda is the Euler–Mascheroni constant. I have written the formula in a slightly different form than is given in the source – actually as I have calculated it. The error is then in the order of $\frac{{2071}}{{398 \cdot 781{{(n + {\textstyle{1 \over 2}})}^6}}}$

I will cut, and round, the result at the corresponding position. All shown digits, except possibly the last, will thus be correct. I have checked the results against Mathematica 8.0.4.0 for some various values.

For the calculations we will use the fact that $\ln (z) = 2{\rm{artanh}}(w) = 2\left( {w + \frac{{{w^3}}}{3} + \frac{{{w^5}}}{5} + ...} \right)$

where $w = \frac{{z - 1}}{{z + 1}}$

If z is fairly close to one, then w will be close to 0, and our series will converge quickly.

We can calculate ln(z) by first calculating an approximate logarithm, here called y, using the ordinary JS-function, Math.log. This will give us an answer with about 16 correct digits. We may then calculate A=z/ey. This will be a value very close to one.  If x is a value approximately equal to z, and y=ln(x) and thus x= ey, then $\ln (z) = \ln (x) + \ln (z) - \ln (x)$

and thus $\ln (x) = y + \ln \left( {\frac{z}{x}} \right) = y + \ln \left( {\frac{z}{{{e^y}}}} \right) = y + \ln \left( A \right)$

We can now calculate ln(A) efficiently using the previously described algorithm. The input is scaled down before the calculations. We have that $\ln (m \cdot {10^n}) = \ln (m) + n \cdot \ln (10)$

The logarithm of m is calculated using the above algorithm, and ln(10) is stored as a constant with 10100 significant figures.

The inverse is calculated using an algorithm (by David W. Cantrell) that I found here: https://oeis.org/A118050.

We have that if z is the searched for sum and $u = {e^{z - \gamma }}$

then $n \approx u - \frac{1}{2} - \frac{1}{{24u}}$

This might be off by one,  but you can always copy the value to the harmonic number calculator, and test the result there, and then adjust it by one if necessary. Up a level : The Harmonic series Last modified: Jan 10, 2022 @ 13:47