RosettaCodeData/Task/Miller-Rabin-primality-test/Mathematica/miller-rabin-primality-test-1.math

10 lines
269 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
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] ]