solutions/cw2/fib.while
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 19 Sep 2023 09:54:41 +0100
changeset 920 7af2eea19646
parent 894 02ef5c3abc51
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
     1
write "Fib: ";
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
read n;  
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
     3
minus1 := 0;
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
     4
minus2 := 1;
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
while n > 0 do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
       temp := minus2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
       minus2 := minus1 + minus2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
       minus1 := temp;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
       n := n - 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
};
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
write "Result: ";
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
write minus2 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13