eratos.pp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993-2005 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. {$ifndef MACOS}
  15. max = 1000000;
  16. {$else}
  17. max = 10000; {Actually it works with 100000 also, but not 1000000,}
  18. {in which case the OS refuses to start it.}
  19. {$endif}
  20. var
  21. a : array[1..max] of boolean;
  22. procedure eratos;
  23. var
  24. i,j : longint;
  25. begin
  26. a[1]:=false;
  27. for i:=2 to max do
  28. a[i]:=true;
  29. for i:=2 to max div 2 do
  30. if a[i] then
  31. for j:=2 to max div i do
  32. a[i*j]:=false;
  33. writeln;
  34. j:=0;
  35. for i:=1 to max do
  36. begin
  37. if a[i] then
  38. begin
  39. write(i:7);
  40. inc(j);
  41. if (j mod 10)=0 then
  42. writeln;
  43. end;
  44. end;
  45. writeln;
  46. end;
  47. begin
  48. write('Calculating the Prime Numbers from 1 to ',max,'...');
  49. eratos;
  50. end.
  51. {
  52. $Log$
  53. Revision 1.3 2005-05-14 11:11:33 olle
  54. * Smaller arrray sizes for macos
  55. Revision 1.2 2002/09/07 15:06:35 peter
  56. * old logs removed and tabs fixed
  57. }