Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Example-Geometric Series Summation Yet Again

In this example we consider the problem of computing a geometric series summation  for the last time. We have already seen two algorithms to compute this summation in Sections gif and gif (Programs gif and gif).

An algorithm to compute a geometric series summation using the closed-form expression (Equation gif) is given in Program gif. This algorithm makes use of Program gif to compute tex2html_wrap_inline58885.

   program1086
Program: Program to compute tex2html_wrap_inline58631 using the closed-form expression

To determine the average running time of Program gif we will make use of Equation gif, which gives the average running time for the Power function which is called on line 5. In this case, the arguments are x and n+1, so the running time of the call to Power is tex2html_wrap_inline58893. Adding to this the additional work done on line 5 gives the average running time for Program gif:

displaymath58883

The running times of the three programs which compute the geometric series summation presented in this chapter are tabulated below in Table gif and are plotted for tex2html_wrap_inline58895 in Figure gif. Figure gif shows that, according to our simplified model of the computer, for n<4, Program gif has the best running time. However as n increases, Program gif is clearly the fastest of the three and Program gif is the slowest for all values of n.

 

 

program T(n)
Program gif tex2html_wrap_inline58903
Program gif 13n+22
Program gif tex2html_wrap_inline58907
Table: Running Times of Programs gif, gif and gif

   figure1123
Figure: Plot of Running Time vs. n for Programs gif, gif and gif


next up previous contents index

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