cwtests/cw03/primes.while
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 30 Nov 2021 10:16:47 +0000
changeset 852 8706b846a3e0
parent 836 a3418ee8c404
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
836
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// prints out prime numbers from 2 to 100 
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
end := 100;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
n := 2;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
while (n < end) do {
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
  f := 2;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
  tmp := 0;
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
  while ((f < n / 2 + 1) && (tmp == 0)) do {
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
    if ((n / f) * f == n) then  { tmp := 1 } else { skip };
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
    f := f + 1
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
  };
852
8706b846a3e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 836
diff changeset
    12
  if (tmp == 0) then { write(n); write("\n") } else { skip };
836
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
  n  := n + 1
a3418ee8c404 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
}