next up previous contents
Next: Differences from the printed Up: Some formulas Previous: Rules for logarithms   Contents

Sums

We often find it useful to abbreviate a sum $ a_1 + \cdots + a_n$ with $ \sum_{i=1}^n a_i$.

We can view this as a little algorithm if we wish: Let us use s to hold the above sum at the end of the program, and assume that double[] a is an array holding the numbers we wish to add, that is: a[i] = $ a_i$.

  double s=0;
  for (i=1; i<=n; i++) 
      s = s + a[i];

The most common use of sums for our purposes is when investigating the complexity of a program. For that, we usually have to count a variant of $ 1 + 2 + \cdots + n$. Hence it is helpful to know that

In order to illustrate this, consider the program

for (i=0; i<n; i++)
{ for(j=0; j<=i; j++)
  { k++ //dummy instruction 1
    l++ //dummy instruction 2
    m++ //dummy instruction 3
  }
}
By the above discussion, its complexity is given as follows:



Martin Escardo 2005-01-11