PROGRAM Prime2(Output);

    {
    ------------------------------------------
    program 8.4 - Use sets to implement Sieve of
    Erastosthenes; represent odd numbers only.
    -------------------------------------------
    }

    CONST
        N = 255;   { N' = N DIV 2 }
    TYPE
        Positive = 1..MaxInt;
    VAR
        Sieve, Primes: SET OF 2..N;
        NextPrime, Multiple, NewPrime, I: Positive;
BEGIN
    Sieve := [2..N];
    Primes := [];
    NextPrime := 2;

    REPEAT
        WHILE NOT (NextPrime IN Sieve) DO
            NextPrime := Succ(NextPrime);

        Primes := Primes + [NextPrime];
        NewPrime := 2 * NextPrime - 1;
        Multiple := NextPrime;

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

    FOR I := 2 TO N DO
        IF I IN Primes THEN
            WriteLn(2 * I - 1)
END.
