The $$n$$-th harmonic number is the sum \[\sum_{i=1}^{n}\frac{1}{i}\]

For smaller values of $$n$$ it is simple to just calculate this sum. However, if $$n$$ if very large, it can take up a lot of time. For larger values of $$n$$, it is better to use an approach that is easy to calculate. You can approach the harmonic numbers using the formula below: \[\ln(n) + \gamma + \frac{1}{2n} - \frac{1}{12n^2} + \frac{1}{120n^4}\]

Here, $$\ln$$ is the natural logarithm1. The $$\gamma$$ in the formula is the Euler-Mascheroni constant2 (not to be mistaken with the Euler number that is represented by $$e$$) and is about equal to 0.577215664901532.

De rode lijn toont de harmonische getallen
The red line shows the harmonic numbers.

Assignment

Example

>>> harmonicExact(10)
2.9289682539682538

>>> harmonicApproach(10)
2.9289682578955776

>>> harmonic(10)
2.9289682539682538