## Harmonic numbers

The *n*th harmonic number, *H*_{n}, is the sum of the reciprocals of the integers up to and including *n*. For example,

*H*_{4} = 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 *H*_{p-1} is divisible by *p*^{2}.

The example above shows this for *p* = 5. In that case, the numerator is not just divisible by *p*^{2}, it *is* *p*^{2}, though this doesn’t hold in general. For example, *H*_{10} = 7381/2520. The numerator 7381 is divisible by 11^{2} = 121, but it’s not equal to 121.

## Generalized harmonic numbers

The generalized harmonic numbers *H*_{n,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 *H*_{p-1,2} is divisible by *p*.

For example, *H*_{4,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 *n*th harmonic number with `harmonic(n)`

and generalized harmonic numbers with `harmonic(n, m)`

.

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 denominators of the first 10 harmonic numbers with

[harmonic(n).as_numer_denom()[0] 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.