Unit 4.15 Computing Sum and Product Expressions

PROG

a)   Compute  

SUM FROM {I=1} TO N I
(u415)
b)   Compute  

PI FROM {I=1} TO N I
(u415a)
c)   Compute  

SUM FROM {I=1} TO N {PI FROM {J=I} TO N J}

(u415b)

PED

To learn how to convert Sigma and Pi notation

CONCEPTS
SUM FROM {I=a} TO b expr(I)
can be translated as:

I:=a;
SUM:=0;
while(I<=b) DO BEGIN
SUM:=SUM+expr(I);
I:=I+1;
end;
PROD FROM {I=a} TO b expr(I)
I:=a;
PRODUCT:=1
while(I<=b) DO BEGIN
PRODUCT:=PRODUCT*expr(I);
I:=I+1;
end;

In the event that sigma's and pi's nested, work from the outside in.



Unit 4.15, Computing Sum and Product Expressions

Sigma and Pi notation shows up frequently in mathematical textbooks, journals and in descriptions of numerical analysis recipes of all kinds. Thus, those of a mathematical bent, should learn how to convert these into PASCAL--what's discussed in this unit.
SUM FROM {I=a} TO b expr(I)
means expr(a) + expr (a+1) ... expr (b)
PROD FROM {I=a} TO b expr(I)
means expr (a) * expr (a+1) ... expr (b)

The first program translates:
SUM FROM {I=1} TO N I
This is simply the sum of the integers from 1 to n. In our template a is 1 and b is n. E.G., if N were 5, this would compute 1+2+3+4+5.
The expr(i) is simply i, so we add i to SUM in the template. Look at line 11.

The second program translates:
PI FROM {I=1} TO N I
This is simply the product of the integers from 1 to n, otherwise known as n!. In our template, a is the 1 and B is N. If N were 5, this would compute 1*2*3*4*5.

The last program computes:
SUM FROM {I=1} TO N {PI FROM {J=I} TO N J}

We have an outer loop from lines 8 to 10 and line 17 to 19. Note that we simply use the sum template. We add product, which is computed in lines 11 to 16 according to the product template we saw earlier.