4. N .. M内的所有素数

4. N .. M内的所有素数
program ex_4;
var
     a : array [2..10000] of boolean;
     n, m, i, t, j, x : integer;
begin
     read(n, m);
     if n > m then
     begin
          t := n;
          n := m;
          m := t;
     end;
     for i := 2 to m do
         a[i] := true;
     x := 1;
     repeat
           x := x+1;
           if a[x] then
           begin
                i := x;
                while i < m do
                begin
                     i := i+x;
                     a[i] := false;
                end;
           end;
     until x*x > m;
     j := 0;
     for i := n to m do
         if a[i] = true then
         begin
              j := j+1;
              if j mod 10 <> 0 then
                 write(i, ' ')
              else
                  writeln(i);
         end;
end.
posted @ 2010-02-25 16:11  SmartIOI  阅读(142)  评论(0)    收藏  举报
本站采用CC授权如需转载、引用文章,请务必附上作者及来源处。 Creative Commons License