Cod sursa(job #152303)

Utilizator Vlad-andreiVlad Fisca Vlad-andrei Data 9 martie 2008 12:36:50
Problema Ciurul lui Eratosthenes Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.09 kb
type vector=array[1..2000000]of 0..1;

var f,g:text;
    ciur:vector;
    s:array[1..1000]of longint;
    i,j,k,n:longint;


begin
     assign(f,'ciur.in');
     assign(g,'ciur.out');
     reset(f);
     rewrite(g);
     read(f,n);
     ciur[1]:=0;
     for i:=2 to n do
         ciur[i]:=1;
     for i:=2 to n do
         if ciur[i]=1 then
            for j:=2 to n div i do
                ciur[i*j]:=0;
     k:=0;
     for i:=2 to n do
         if ciur[i]=1 then
         begin
              inc(k);
         end;
     if k>1000 then
     begin
          writeln(g,k);
          j:=0;
          i:=n;
          repeat
                if ciur[i]=1 then
                begin
                     j:=j+1;
                     s[j]:=i;
                end;
                i:=i-1;
          until j=1000;
          for j:=1000 downto 1 do
              write(g,s[j],' ');
     end else
         begin
              writeln(g,k);
              for i:=2 to n do
                  if ciur[i]=1 then write(g,i,' ');
         end;
     close(f);
     close(g);
end.