cwtests/cw02/fib.while
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 03 Dec 2021 17:45:11 +0000
changeset 853 568671822d52
parent 833 aad5957eb7e4
child 934 ee35eeb5831a
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: 833
diff changeset
     1
write "Fib: ";
833
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
read n;  
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
minus1 := 0;
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
minus2 := 1;
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
while n > 0 do {
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
       temp := minus2;
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
       minus2 := minus1 + minus2;
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
       minus1 := temp;
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
       n := n - 1
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
};
853
568671822d52 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 833
diff changeset
    11
write "Result: ";
568671822d52 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 833
diff changeset
    12
write minus2 ;
568671822d52 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 833
diff changeset
    13
write "\n"
833
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14