Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

About Harmonic Numbers

 

The series tex2html_wrap_inline57919 is called the harmonic series , and the summation

displaymath57915

gives rise to the series of harmonic numbers , tex2html_wrap_inline57921, tex2html_wrap_inline57923, .... As it turns out, harmonic numbers often creep into the analysis of algorithms. Therefore, we should understand a little bit about how they behave.

A remarkable characteristic of harmonic numbers is that, even though as n gets large and the difference between consecutive harmonic numbers gets arbitrarily small ( tex2html_wrap_inline57927), the series does not converge! That is, tex2html_wrap_inline57929 does not exist. In other words, the summation tex2html_wrap_inline57931 goes off to infinity, but just barely.

Figure gif helps us to understand the behavior of harmonic numbers. The smooth curve in this figure is the function y=1/x. The descending staircase represents the function tex2html_wrap_inline57935.gif

   figure632
Figure: Computing harmonic numbers.

Notice that the area under the staircase between 1 and n for any integer n>1 is given by

eqnarray735

Thus, if we can determine the area under the descending staircase in Figure gif, we can determine the values of the harmonic numbers.

As an approximation, consider the area under the smooth curve y=1/x:

eqnarray747

Thus, tex2html_wrap_inline57987 is approximately tex2html_wrap_inline57989 for n>1.

If we approximate tex2html_wrap_inline57987 by tex2html_wrap_inline57989, the error in this approximation is equal to the area between the two curves. In fact, the area between these two curves is such an important quantity that it has its own symbol, tex2html_wrap_inline57997 , which is called Euler's constant . The following derivation indicates a way in which to compute Euler's constant:

eqnarray760

A program to compute Euler's constant on the basis of this derivation is given in Program gif. While this is not necessarily the most accurate or most speedy way to compute Euler's constant, it does give the correct result to six significant digits.

   program789
Program: Program to compute tex2html_wrap_inline57997.

So, with Euler's constant in hand, we can write down an expression for the tex2html_wrap_inline58003 harmonic number:

  equation797

where tex2html_wrap_inline58005 is the error introduced by the fact that tex2html_wrap_inline57997 is defined as the difference between the curves on the interval tex2html_wrap_inline58009, but we only need the difference on the interval [1,n]. As it turns out, it can be shown (but not here), that there exists a constant K such that for large enough values of n, tex2html_wrap_inline58017.gif

Since the error term is less than 1/n, we can add 1/n to both sides of Equation gif and still have an error which goes to zero as n gets large. Thus, the usual approximation for the harmonic number is

displaymath57916

We now return to the question of finding the average running time of Program gif, which finds the largest element of an array. We can now rewrite Equation gif to give

eqnarray807


next up previous index

Bruno Copyright © 2003, 2004 by Bruno R. Preiss, P.Eng. All rights reserved.