Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program prime_numbers;
- type TList = array of word;
- function isPrime(n : word) : Boolean;
- var i : word;
- begin
- isPrime := true;
- for i in [2..n div 2] do
- if n mod i = 0 then begin
- isPrime := false;
- break;
- end;
- end;
- function listNPrimes(n : word) : TList;
- var i, count : word;
- begin
- SetLength(listNPrimes, 1);
- if n >= 1 then begin
- count := 0;
- listNPrimes[0] := 2;
- i := 3;
- while (count < n) do begin
- if isPrime(i) then begin
- SetLength(listNPrimes, length(listNPrimes)+1);
- listNPrimes[length(listNPrimes)-1] := i;
- inc(count);
- end;
- inc(i);
- end;
- end else listNPrimes[0] := 0;
- end;
- function listPrimesTill(n : word) : TList;
- var i : word = 3;
- begin
- SetLength(listPrimesTill, 1);
- if n >= 2 then begin
- listPrimesTill[0] := 2;
- while not(i >= n) do begin
- if isPrime(i) then begin
- SetLength(listPrimesTill, length(listPrimesTill)+1);
- listPrimesTill[length(listPrimesTill)-1] := i;
- end;
- inc(i);
- end;
- end else listPrimesTill[0] := 0;
- end;
- var elem : integer;
- begin
- for elem in listNPrimes(10) do write(elem,', '); writeln;
- for elem in listPrimesTill(40) do write(elem,', ');
- readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment