cwtests/cw03/fib.while
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 04 Dec 2021 00:41:31 +0000
changeset 859 f766d3486c9d
parent 853 568671822d52
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
853
568671822d52 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 836
diff changeset
     1
write "Fib: ";
836
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
read n;  
859
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 853
diff changeset
     3
minus1 := 1;
f766d3486c9d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 853
diff changeset
     4
minus2 := 0;
836
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
while n > 0 do {
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
       temp := minus2;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
       minus2 := minus1 + minus2;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
       minus1 := temp;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
       n := n - 1
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
};
853
568671822d52 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 836
diff changeset
    11
write "Result: ";
568671822d52 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 836
diff changeset
    12
write minus2 ;
568671822d52 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 836
diff changeset
    13
write "\n"
836
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14