program EratosthenesSieve;
{$APPTYPE CONSOLE}
uses
SysUtils, DateUtils,Math;
const
version ='1.0.1d1';
N = 1000000000;
var
sieve : array [2..N] of boolean;
t0, t1,dt : TDateTime;
O,C : Extended;
procedure init;
var
i : integer;
begin
for i:=2 to n do
sieve [i] := not odd(i);
end;
procedure calc (start : integer);
var
prime,prime2, i : integer;
breakLoop, exitProc : Boolean;
begin
prime := start;
exitProc := false;
repeat
prime := prime+1;
while (prime<N) and sieve[prime] do
inc (prime);
i:= sqr(prime);
prime2 := prime+prime;
if i<= N then
begin
breakLoop := false;
repeat
if i<= N then
begin
sieve [i] := true;
inc (i,prime2);
end
else
breakLoop := true;
until breakLoop;
end
else
exitProc := true;
until exitProc;
sieve [2] := false;
end;
procedure print;
var
i :integer;
found : integer;
begin
found := 0;
for i:=2 to N do
if not sieve [i] then
begin
inc(found);
end;
writeln;
writeln ('Found ',found,' primes.');
end;
begin
writeln ('Sieve of Eratosthenes version ', version);
writeln('N= ',N);
init;
t0 := now;
writeln('Program started ',DateTimeToStr(t0));
t0 := now;
calc (2);
t1 := now;
writeln('Program finished ',DateTimeToStr(t1));
dt := SecondSpan(t1,t0);
writeln ('Time is ',FloatToStr(dt),' sec.');
O := N* ln(ln(N));
C := dt/O;
writeln ('O(N ln ln N)= ',O,' C=',C);
O := sqr(N)/ln(N);
C := dt/O;
writeln ('O(N*N/ln N)= ',O,' C=',C);
print;
writeln ('Press Enter to stop...');
readln;
end.