cwtests/cw02/factors.while
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 04 Sep 2021 14:09:26 +0100
changeset 835 08b157566a73
parent 833 aad5957eb7e4
child 853 568671822d52
permissions -rw-r--r--
cwupdates
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
833
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Find all factors of a given input number
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
write "Input n please";
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
read n;
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
write "The factors of n are";
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
f := 2;
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
while (f < n / 2 + 1) do {
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
  if ((n / f) * f == n) then  { write(f) } else { skip };
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
  f := f + 1
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
}