cwtests/cw04/fib.while
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 29 Nov 2024 18:59:32 +0000
changeset 976 e9eac62928f5
parent 859 f766d3486c9d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
859
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
     1
write "Fib: ";
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
     2
read n;  
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
     3
minus1 := 1;
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
     4
minus2 := 0;
836
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
while n > 0 do {
859
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
     6
       temp := minus2;
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
     7
       minus2 := minus1 + minus2;
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
     8
       minus1 := temp;
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
     9
       n := n - 1
836
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
};
859
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
    11
write "Result: ";
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
    12
write minus2 ;
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
    13
write "\n"
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 852
diff changeset
    14