## 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 numerators 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.

Let s(n) be the sum of the divisors of n. The Riemann Hypothesis is equivalent to Problem E: for all n greater than 1, s(n) < H_n + exp(H_n) log(H_n). This is a result of Jeff Lagarias. (http://www.math.lsa.umich.edu/~lagarias/doc/elementaryrh.pdf)

Very interesting!

Wrote a code snippet to test for primes using this property.

For fun I decided to create the harmonic function from what I just read, in Perl 6.

After maybe five minutes of testing and iterating on the REPL I came up with:

( That includes the amount of time I spent playing with the code before trying to put it in a subroutine )

Since that is a bit dense, this is what it would look like if I expanded it out a bit, and added comments for someone new to Perl 6:

If you want just the numerators for the first ten harmonic numbers

`my @n = (harmonic($_).numerator for ^10);`

If you want to format the output as a fraction:

`say harmonic(10).nude.join('/');`

Have you seen the chapter of

Proofs That Really Countby Benjamin and Quinn that is all about combinatorial interpretations of harmonic numbers? I found it pretty amazing that such things could even exist at all! It’s a book I think you would enjoy, if you haven’t seen it, and it could give you a lot to add to this post.The p^2 divisibility reminds me of Eisenstein’s irreducibility criterion. I wonder if there’s a connection.