RosettaCodeData/Task/Miller-Rabin-primality-test/Mathematica/miller-rabin-primality-test-1.math
2023-07-01 13:44:08 -04:00

9 lines
269 B
Text

MillerRabin[n_,k_]:=Module[{d=n-1,s=0,test=True},While[Mod[d,2]==0 ,d/=2 ;s++]
Do[
a=RandomInteger[{2,n-1}]; x=PowerMod[a,d,n];
If[x!=1,
For[ r = 0, r < s, r++, If[x==n-1, Continue[]]; x = Mod[x*x, n]; ];
If[ x != n-1, test=False ];
];
,{k}];
Print[test] ]