annotate lab3/sumpow.p @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* lab3/sumpow.p *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 proc sumpow(n, k);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 var m, s;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 proc pow(p);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 var j, q;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 j := 0; q := 1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 while j < k do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 j := j+1; q := q*p
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 return q
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 m := 0; s := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 while m < n do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 m := m + 1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 s := s + pow(m)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 return s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 print sumpow(5, 4); newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 979
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 >>*)