The nth harmonic number, Hn, is the sum of the reciprocals of the integers up to and including n. For example,
H4 = 1 + 1/2 + 1/3 + 1/4 = 25/12.
Here’s a curious fact about harmonic numbers, known as Wolstenholme’s theorem:
For a prime p > 3, the numerator of Hp-1 is divisible by p2.
The example above shows this for p = 5. In that case, the numerator is not just divisible by p2, it is p2, though this doesn’t hold in general. For example, H10 = 7381/2520. The numerator 7381 is divisible by 112 = 121, but it’s not equal to 121.
Generalized harmonic numbers
The generalized harmonic numbers Hn,m are the sums of the reciprocals of the first n positive integers, each raised to the power m. Wolstenholme’s theorem also says something about these numbers too:
For a prime p > 3, the numerator of Hp-1,2 is divisible by p.
For example, H4,2 = 205/144, and the numerator is clearly divisible by 5.
Computing with Python
You can play with harmonic numbers and generalized harmonic numbers in Python using SymPy. Start with the import statement
from sympy.functions.combinatorial.numbers import harmonic
Then you can get the nth harmonic number with
harmonic(n) and generalized harmonic numbers with
To extract the numerators, you can use the method
as_numer_denom to turn the fractions into (numerator, denominator) pairs. For example, you can create a list of the numerators of the first 10 harmonic numbers with
[harmonic(n).as_numer_denom() for n in range(10)]
What about 0?
You might notice that
harmonic(0) returns 0, as it should. The sum defining the harmonic numbers is empty in this case, and empty sums are defined to be zero.