Miller–Rabin primality test: Difference between revisions

add Ada
(Pari/GP)
(add Ada)
Line 20:
* The nature of the test involves big numbers, so the use of "big numbers" libraries (or similar features of the language of your choice) are suggested, but '''not''' mandatory.
* Deterministic variants of the test exist and can be implemented as extra (not mandatory to complete the task)
 
=={{header|Ada}}==
It's easy to get overflows doing exponential calculations. Therefore I implemented my own function for that.
 
For Number types >= 2**64 you may have to use an external library.
 
<lang Ada>with Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
 
procedure Miller_Rabin is
-- New Type for Number, for easy swapping with bigger type if necessary.
-- Limit to 2**48 for now, since Discrete_Random in GNAT GPL 2010 doesn't
-- guarantee statistical properties for size > 48.
-- RM requires them to be guaranteed only up to 2**15.
type Number is mod 2**48;
 
package Num_IO is new Ada.Text_IO.Modular_IO (Number);
package Pos_IO is new Ada.Text_IO.Integer_IO (Positive);
 
type Result_Type is (Composite, Probably_Prime);
 
function Is_Prime (N : Number; K : Positive := 10)
return Result_Type
is
subtype Number_Range is Number range 2 .. N - 1;
package Random is new Ada.Numerics.Discrete_Random (Number_Range);
 
function Mod_Exp (Base, Exponent, Modulus : Number) return Number is
Result : Number := 1;
begin
for E in 1 .. Exponent loop
Result := Result * Base mod Modulus;
end loop;
return Result;
end Mod_Exp;
 
Generator : Random.Generator;
D : Number := N - 1;
S : Natural := 0;
X : Number;
begin
-- exclude 2 and even numbers
if N = 2 then
return Probably_Prime;
elsif N mod 2 = 0 then
return Composite;
end if;
 
-- write N-1 as 2**S * D, with D mod 2 /= 0
while D mod 2 = 0 loop
D := D / 2;
S := S + 1;
end loop;
 
-- initialize RNG
Random.Reset (Generator);
for Loops in 1 .. K loop
X := Mod_Exp(Random.Random (Generator), D, N);
if X /= 1 and X /= N - 1 then
Inner : for R in 1 .. S - 1 loop
X := Mod_Exp (X, 2, N);
if X = 1 then return Composite; end if;
exit Inner when X = N - 1;
end loop Inner;
if X /= N - 1 then return Composite; end if;
end if;
end loop;
 
return Probably_Prime;
end Is_Prime;
 
-- start main procedure
N : Number;
K : Positive;
begin
 
for I in 2 .. 1000 loop
if Is_Prime (Number(I)) = Probably_Prime then
Ada.Text_IO.Put (Integer'Image (I));
end if;
end loop;
Ada.Text_IO.Put_Line (".");
 
Ada.Text_IO.Put ("Enter a Number: ");
Num_IO.Get (N);
Ada.Text_IO.Put ("Enter the count of loops: ");
Pos_IO.Get (K);
Ada.Text_IO.Put_Line ("What is it? " & Result_Type'Image (Is_Prime (N, K)));
end Miller_Rabin;</lang>
 
Output:
<pre> 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997.
Enter a Number: 1234567
Enter the count of loops: 20
What is it? COMPOSITE</pre>
 
=={{header|ALGOL 68}}==
256

edits