progs/while-tests/primes.while
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 09 Dec 2021 00:07:51 +0000
changeset 862 3ea5ba4bc3b4
parent 750 e93a9e74ca8e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
750
e93a9e74ca8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     1
// prints out prime numbers from 2 to 100 
674
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
end := 100;
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
n := 2;
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
while (n < end) do {
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
  f := 2;
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
  tmp := 0;
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
  while ((f < n / 2 + 1) && (tmp == 0)) do {
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
    if ((n / f) * f == n) then  { tmp := 1 } else { skip };
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
    f := f + 1
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  };
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  if (tmp == 0) then { write(n) } else { skip };
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  n  := n + 1
e0a41a1f24cf updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
}