cwtests/cw02/factors.while
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 01 Oct 2023 13:35:51 +0100
changeset 934 ee35eeb5831a
parent 860 6f80e6df34f7
permissions -rw-r--r--
updated
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
934
ee35eeb5831a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 860
diff changeset
     2
// by J.R. Cordy August 2005
833
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
934
ee35eeb5831a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 860
diff changeset
     4
write "Input n please";
833
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
read n;
934
ee35eeb5831a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 860
diff changeset
     6
write "The factors of n are:\n";
833
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 {
860
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 856
diff changeset
     9
  if ((n / f) * f == n)
934
ee35eeb5831a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 860
diff changeset
    10
  then  { write(f); write "\n" }
860
6f80e6df34f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 856
diff changeset
    11
  else { skip };
833
aad5957eb7e4 cwupdates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
  f := f + 1
934
ee35eeb5831a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 860
diff changeset
    13
}