Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uses math, SysUtils;
- type
- IntArray = Array of Int64;
- function IsMersennePrime(MersennePrime: Int64; Exponent: Integer): Boolean;
- var
- IsPrime: Boolean;
- S: Int64;
- I: Integer;
- begin
- S := 4;
- IsPrime := false;
- Dec(Exponent);
- for I := 2 to Exponent do
- S := ((S * S) - 2) mod MersennePrime;
- if (S = 0) or (MersennePrime = 3) then
- IsPrime := true;
- IsMersennePrime := IsPrime;
- end;
- function GetPrimes(): IntArray;
- Const
- P: Byte = 64;
- Var
- Ret: IntArray;
- I, J: Integer;
- begin
- SetLength(Ret, P);
- for I := 0 to High(Ret) do
- Ret[I] := I;
- for I := 2 to High(Ret) do
- if (Ret[I] <> 0) then
- for J := I + 1 to High(Ret) do
- if Ret[J] mod Ret[I] = 0 then
- Ret[J] := 0;
- GetPrimes := Ret;
- end;
- function GetN(): Integer;
- Var
- IsCorrect: Boolean;
- Ret: Int64;
- begin
- repeat
- IsCorrect := true;
- Writeln('Введите N');
- try
- Readln(Ret);
- except
- Writeln('N должно быть числом');
- IsCorrect := false;
- end;
- if (Ret < 1) and IsCorrect then
- begin
- Writeln('N должно быть целым числом большим нуля');
- IsCorrect := false;
- end;
- until IsCorrect;
- GetN := Ret;
- end;
- procedure PrintArray(ArrayToPrint: IntArray);
- var
- I: Integer;
- begin
- Writeln('Числа мерсена равны ');
- for I := 0 to High(ArrayToPrint) do
- if ArrayToPrint[I] <> 0 then
- Writeln(ArrayToPrint[I]);
- end;
- procedure GetMersennePrimes();
- var
- MersennePrimes, Primes: IntArray;
- I, N: Integer;
- MersennePrime: Int64;
- begin
- N := GetN();
- Primes := GetPrimes();
- SetLength(MersennePrimes, Length(Primes));
- for I := 0 to High(MersennePrimes) do
- begin
- MersennePrime := Round(Power(2, Primes[I]) - 1);
- if IsMersennePrime(MersennePrime, Primes[I]) and (MersennePrime < N)
- then
- MersennePrimes[I] := MersennePrime;
- end;
- PrintArray(MersennePrimes);
- end;
- begin
- GetMersennePrimes();
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement