annotate lab3/sumpow2.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/sumpow2.p *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 proc sum(n, f);
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 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 m := 0; s := 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 while m < n do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 m := m + 1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 s := s + f(m)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 return s
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 proc sumpow(n, k);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 proc pow(p);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 var j, q;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 j := 0; q := 1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 while j < k do
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 j := j+1; q := q*p
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 return q
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 return sum(n, pow)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 print sumpow(5, 4); newline
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 end.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 (*<<
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 979
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 >>*)