Numerators of harmonic numbers

Harmonic numbers

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


7 thoughts on “Numerators of harmonic numbers

  1. Brad Gilbert (b2gills)

    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:

    sub harmonic ( UInt $n, UInt $m = 1 --> Rational ){
      ( [+] ({ 1 / ++$ ** $m } ... *)[^$n] ).Rat

    ( 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:

    sub harmonic ( UInt $n, UInt $m = 1 ) returns Rational {
      # Generate an infinite list of reciprocals that are raised to the power of `$m`.
      # The `state` variable will maintain its value for each call of the block
      # within this sequence. A new one is created for each new list.
      my @reciprocals = { 1 / ++( state $unnamed = 0 ) ** $m } ... *;
      # `1 / 2` returns a rational value
      # (up to some arbitrarily huge denominator above uint64.Range.max )
      # (^$n)   eqv   (0 ..^ $n)   eqv 0, $n, :excludes-max )
      # `^$n` is typically called the `up to` operator
      # `[ «OPERATOR» ] «LIST»` is the list reduction metaoperator
      # which respects the left/right associativity of the infix operator
      my $sum = [+] @reciprocals[ ^$n ];
      # `[<=] 1, 2, 3, 4` it can be used to make sure a list is sorted
      # note that `[+] ()` returns `0` because
      # &infix:«+» with zero arguments returns `0`
      # ensure that it returns a rational number
      # only needed for the $n == 0 case
      return $sum.Rat;

    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('/');

  2. Have you seen the chapter of Proofs That Really Count by 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.

Leave a Reply

Your email address will not be published. Required fields are marked *