Skip to content

Instantly share code, notes, and snippets.

@Kedrigern
Created October 21, 2012 17:21
Show Gist options
  • Select an option

  • Save Kedrigern/3927671 to your computer and use it in GitHub Desktop.

Select an option

Save Kedrigern/3927671 to your computer and use it in GitHub Desktop.
Prvočísla (test, eratoshenovo síto) v Pascalu....
{Author: Ondřej Profant, 2009}
unit prvocisla;
interface
Const MaxN = 10000; // konec Eratosthenova Síta
Type SITO = array [2..MaxN] of boolean;
function Prvocislo(j: integer): boolean;
function EratosthenovoSito: SITO;
implementation
{Funkce, která řekne, zda je dané číslo prvočíslo}
function Prvocislo(j: integer): boolean;
var i : integer;
begin
{test}
if j < 1 then WriteLn('Záporné číslo, nula, či přetečení integeru.')
else
begin
for i:=2 to (Round(Sqrt(j))) do
begin
if j mod i <> 0 then continue
else
begin
Prvocislo := False;
Exit;
end;
end;
end;
Prvocislo := True;
end;
{Funkce, která vrátí pole s vyznačenými (true) prvočísly}
{Pole je deklarované typem SITO}
function EratosthenovoSito: SITO;
var i,j : integer;
begin
{Inicializace}
for i:=2 to MaxN do EratosthenovoSito[i] := True;
{Samotné síto}
i:=2;
while i <= round(sqrt(MaxN)) do
begin
{Již označená čísla}
while ((i<MaxN) and (not EratosthenovoSito[i])) do i := i + 1;
{Násobky}
j := 2;
while i*j <= MaxN do
begin
EratosthenovoSito[(j*i)] := false;
j := j+1;
end;
i := i+1;
end;
end;
BEGIN END.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment