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

An example-Geometric Series Summation

 

In this section we consider the running time of a program to compute the following geometric series summation . That is, given a value x and non-negative integer n, we wish to compute the summation

displaymath58075

An algorithm to compute this summation is given in Program gif.

   program872
Program: Program to compute tex2html_wrap_inline58081.

Table gif gives the running time, as predicted by the simplified model, for each of the executable statements in Program gif.

 

 

statement time
2 2
3 2
4 3(n+2)
5 2(n+1)
6 2(n+1)
7 tex2html_wrap_inline58093
8 tex2html_wrap_inline58095
9 tex2html_wrap_inline58095
10 4(n+1)
11 4(n+1)
12 2
TOTAL tex2html_wrap_inline58105
Table: Computing the running time of Program gif.

In order to calculate the total cycle counts, we need to evaluate the two series summations tex2html_wrap_inline58107 and tex2html_wrap_inline58109. Both of these are arithmetic series summations . In the next section we show that the sum of the series tex2html_wrap_inline58111 is n(n+1)/2. Using this result we can sum the cycle counts given in Table gif to arrive at the total running time of tex2html_wrap_inline58105 cycles.


next up previous index

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