Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.66 KB | None | 0 0
  1. program cons;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4.  
  5. uses
  6.   System.SysUtils;
  7.  
  8. type
  9.     TIntArray = array of Integer;
  10.  
  11. function FastExp(a, z, m: Int64): Integer;
  12. var
  13.     R: Int64;
  14. begin
  15.     R := 1;
  16.     while z <> 0 do
  17.     begin
  18.         while (z and 1) = 0 do
  19.         begin
  20.             z := z shr 1;
  21.             a := (a * a) mod m;
  22.         end;
  23.         z := z - 1;
  24.         R := (R * a) mod m;
  25.     end;
  26.     Result := R;
  27. end;
  28.  
  29. function Factorize(N: Integer): TIntArray;
  30. var
  31.     I, Count: Integer;
  32. begin
  33.     SetLength(Result, Trunc(Sqrt(N)));
  34.     Count := 0;
  35.     for I := 2 to Trunc(Sqrt(N)) do
  36.     begin
  37.         if N mod I = 0 then
  38.         begin
  39.             Result[Count] := I;
  40.             Inc(Count);
  41.             while N mod I = 0 do
  42.                 N := N div I;
  43.         end;
  44.     end;
  45.     if N <> 1 then
  46.     begin
  47.         Result[Count] := N;
  48.         Inc(Count);
  49.     end;
  50.     SetLength(Result, Count);
  51. end;
  52.  
  53. function AnyPhi(N: Integer): Integer;
  54. var
  55.     Factors: TIntArray;
  56.     I: Integer;
  57. begin
  58.     Result := N;
  59.     Factors := Factorize(N);
  60.     for I := Low(Factors) to High(Factors) do
  61.     begin
  62.         Result := Result * (Factors[I] - 1) div Factors[I];
  63.     end;
  64. end;
  65.  
  66. function FindPrimitiveRoots(P: Integer): TIntArray;
  67. var
  68.     Factors: TIntArray;
  69.     G, I, Phi, Count: Integer;
  70.     IsCorrect: Boolean;
  71. begin
  72.     Phi := P - 1;
  73.     Count := 0;
  74.     Factors := Factorize(Phi);
  75.     SetLength(Result, AnyPhi(Phi));
  76.     for G := 2 to Phi do
  77.     begin
  78.         IsCorrect := True;
  79.         I := Low(Factors);
  80.         while IsCorrect and (I <= High(Factors)) do
  81.         begin
  82.             IsCorrect := IsCorrect and (FastExp(G, Phi div Factors[I], p) <> 1);
  83.             Inc(I);
  84.         end;
  85.         if IsCorrect then
  86.         begin
  87.             Result[Count] := G;
  88.             Inc(Count);
  89.         end;
  90.     end;
  91.  
  92.     //
  93. //    SetLength(Result, Count);
  94. end;
  95.  
  96. function IsPrime(N: Integer): Boolean;
  97. var
  98.     I: Integer;
  99. begin
  100.     Result := True;
  101.     I := 2;
  102.     while Result and (I < Trunc(Sqrt(N))) do
  103.     begin
  104.         Result := Result and (N mod I <> 0);
  105.         Inc(I);
  106.     end;
  107. end;
  108.  
  109. function GCD(A, B: Integer): Integer;
  110. begin
  111.     if B = 0 then
  112.         Result := A
  113.     else
  114.         Result := GCD(B, A mod B);
  115. end;
  116.  
  117. function IsRelativelyPrime(A, B: Integer): Boolean;
  118. begin
  119.     Result := GCD(A, B) = 1;
  120. end;
  121.  
  122. var
  123.   a: TIntArray;I: Integer;
  124.  
  125. begin
  126. writeln(IsPrime(17));
  127.  
  128.   a := FindPrimitiveRoots(115249);
  129. //  SetLength(a, 0);
  130. //Writeln(AnyPhi(17*3));
  131. //a:=Factorize(911);
  132. //  a := FindPrimitiveRoots(911);
  133.   writeln('Number of primitives ', Length(a));
  134.   for I := 0 to 500 do
  135.     Write(a[I], ' ');
  136. Readln;
  137. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement