eratos.pp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993-98 by Florian Klaempfl
  5. Eratos Example, Calculates all Prime Numbers from 1 to max
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. program eratosthenes;
  13. const
  14. max = 1000000;
  15. var
  16. a : array[1..max] of boolean;
  17. procedure eratos;
  18. var
  19. i,j : longint;
  20. begin
  21. a[1]:=false;
  22. for i:=2 to max do
  23. a[i]:=true;
  24. for i:=2 to max div 2 do
  25. if a[i] then
  26. for j:=2 to max div i do
  27. a[i*j]:=false;
  28. writeln;
  29. j:=0;
  30. for i:=1 to max do
  31. begin
  32. if a[i] then
  33. begin
  34. write(i:7);
  35. inc(j);
  36. if (j mod 10)=0 then
  37. writeln;
  38. end;
  39. end;
  40. writeln;
  41. end;
  42. begin
  43. write('Calculating the Prime Numbers from 1 to ',max,'...');
  44. eratos;
  45. end.
  46. {
  47. $Log$
  48. Revision 1.2 2002-09-07 15:06:35 peter
  49. * old logs removed and tabs fixed
  50. }