Article 2KF1S Computing harmonic numbers

Computing harmonic numbers

by
John
from John D. Cook on (#2KF1S)

The harmonic numbers are defined by

harmonic.png

Harmonic numbers are sort of a discrete analog of logarithms since

logn.png

As n goes to infinity, the difference between Hn and log n is Euler's constant I^3 = 0.57721" [1]

How would you compute Hn? For small n, simply use the definition. But if n is very large, there's a way to approximate Hn without having to do a large sum.

Since in the limit Hn - log n goes to I^3, a crude approximation would be

harmonic_approx0.png

But we could do much better by adding a couple terms to the approximation above. [2] That is,

harmonic_approx.png

The error in the approximation above is between 0 and 1/120n4.

So if you used this to compute the 1000th harmonic number, the error would be less than one part in 120,000,000,000,000. Said another way, for n = 1000 the approximation differs from the exact value in the 15th significant digit, approximately the resolution of floating point numbers (i.e. IEEE 754 double precision).

And the formula is even more accurate for larger n. If we wanted to compute the millionth harmonic number, the error in our approximation would be somewhere around the 26th decimal place.

* * *

[1] See Julian Havil's excellent Gamma: Exploring Euler's Constant. It's popular-level book, but more sophisticated than most such books.

q?_encoding=UTF8&ASIN=B009P09JLM&Format=ir?t=theende-20&l=li3&o=1&a=B009P09JLM

[2] There's a sequence of increasingly accurate approximations that keep adding reciprocals of even powers of n, based on truncating an asymptotic series. See Concrete Mathematics for details.

q?_encoding=UTF8&ASIN=0201558025&Format=ir?t=theende-20&l=li3&o=1&a=0201558025

GTnj17A_rqc
External Content
Source RSS or Atom Feed
Feed Location http://feeds.feedburner.com/TheEndeavour?format=xml
Feed Title John D. Cook
Feed Link https://www.johndcook.com/blog
Reply 0 comments