solutions/cw3/primes.while
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 30 Oct 2023 12:58:45 +0000
changeset 949 61797848eede
parent 894 02ef5c3abc51
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// prints out prime numbers from
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
// 2 to 100 (end)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
end := 100;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
n := 2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
while (n < end) do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
  f := 2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
  tmp := 0;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
  while ((f < n / 2 + 1) && (tmp == 0)) do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
    if ((n / f) * f == n) then  { tmp := 1 } else { skip };
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
    f := f + 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
  };
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
  if (tmp == 0) then { write(n); write("\n") } else { skip };
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
  n  := n + 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
}