PROGRAM Prime1(Output);

    {------------------------------------
      Program 8.3 - use sets to implement
      Sieve of Erastosthenes.
     ------------------------------------}

    CONST
        N = 255;
    TYPE
        Positive = 1..MaxInt;
    VAR
        Sieve, Primes: SET OF 2..N;
        NextPrime, Multiple, I: Positive;

BEGIN
    Sieve := [2..N];
    Primes := [];
    NextPrime := 2;

    REPEAT          { find next prime }
        WHILE NOT (NextPrime IN Sieve) DO
            NextPrime := Succ(NextPrime);

        Primes := Primes + [nextPrime];
        Multiple := NextPrime;

        WHILE Multiple <= N DO
        BEGIN
            Sieve := Sieve - [Multiple];
            Multiple := Multiple + NextPrime
        END
    UNTIL Sieve = [];

    { Write prime numbers on screen }
    FOR I := 2 TO N DO
        IF I IN Primes THEN
            WriteLn(I);
END.
