solution/cw3/primes.while
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 10 Oct 2022 15:15:15 +0100
changeset 888 fc812b8f120f
parent 864 b5b1bc0a603b
permissions -rw-r--r--
texupdate
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
}