cwtests/cw04/fib.while
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 09 Sep 2023 14:14:31 +0100
changeset 916 10f834eb0a9e
parent 859 f766d3486c9d
permissions -rw-r--r--
texupdate
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