It is known by the Fermat theorem that if p is a prime number and A is an integer, then a^p==a (mod p) is satisfied. If there is a positive integer A does not satisfy A^p==a (mod p), then n is composite.
Definition: A is a positive integer, if p is composite and satisfies a^p==a (mod p), then p is called A-based pseudo prime.
miller-rabin Prime number test algorithm principle : if p is a prime number and (A,p) ==1, (A is any positive integer less than P), then a^p-1==1 (mod p). If A^p-1==1 (mod p),
It can be considered that n is a prime number, and multiple bottoms are tested, the more times, the greater the probability of n being prime. (My personal understanding of the multiple tests for P-based, to make it a pseudo-prime probability greatly reduced).
(miller-rabin Test : Constantly select not more than n-1 of base B (s), calculate whether each time there is bn-1≡1 (mod n), if each time is established n is a prime number, otherwise, composite. )
reprint: said Miller-rabin Test before the first two more efficient to find a*b% N and ab%n functions, This is all about binary thinking, splitting B into binary, and then adding (multiplying)
// A * b% n // For example: B = 1011101 so a * b mod n = (A * 1000000 mod n + A * 10000 mod n + A * $ mod n + A * 1 MoD n + A * 1 mod n) mod n ll Mod_mul (ll A, ll B, ll N) {ll res = 0 ; while (b) { if (B&1 ) res = (res + a)% n; A = (A + a)% n;//a= (a<<1)%n b >>= 1 ; return res;
This code is very good, in the future when calculating a*b, if there is a large number, you can choose the above algorithm, (NLOGN) time complexity.
// a^b% n // similarly ll Mod_exp (ll A, ll B, ll N) { 1; while (b) { if(b&1) res = Mod_mul (res, a, n); = Mod_mul (A, A, n); 1 ; } return Res;}
Fast power, nothing to say.
Core code:
You need to add Srand (Time (NULL)) to start the program;
bool Miller_rabin (ll N) { for (int i=1 ; i<=n; i++) // n is the number of times you intend to test, N (10~20) {ll a =r Andom (N-2 ) +1 ; // if (Mod_exp (A,n-1 , MoD)!=1 composite " ; } }}
note that the Miller-rabin test is probabilistic, not deterministic, but because the probability of an error is very small after multiple runs, the actual application is still feasible. (The probability of a miller-rabin test of its success is 3/4)
Two-time detection theorem : (improved)
A composite n, if there is a b^n-1==1 (mod n) for all positive integer b that satisfies the (b,n) =1, (the inverse of the above, but the probability of such a number is small), is called the Carmichael number.
two-time detection if P is an odd prime, then the solution for X2≡1 (mod p) is x = 1 | | x = p-1 (mod p);
You can use the two-time detection theorem to add some detail to the implementation of Miller-rabin, which is implemented as follows:
BOOLMiller_rabin (ll N) {if(n = =2|| n = =3|| n = =5|| n = =7|| n = = One)return true; if(n = =1|| ! (n%2) || ! (n%3) || ! (n%5) || ! (n%7) || ! (n% One))return false; ll x, pre, U; intI, j, k =0; U= N-1;//requirement x^u% n while(! (u&1)) {//if u is an even number, you move right, and you record shift numbers with Kk++; U >>=1; } srand ((ll) Time (0)); for(i =0; i < S; ++i) {//Conduct s Testx = rand ()% (n2) +2;//take a random number in [2, N] if((x%n) = =0)Continue; X= Mod_exp (x, u, n);//calculate (x^u)% n First,Pre =x; for(j =0; J < K; ++J) {//fill in the amount of displacement minus, and add two probes to this place .x =Mod_mul (x, x, N); if(x = =1&& Pre! =1&& Pre! = N1)return false;//Two-time detection theorem, here if x = 1 The pre must be equal to 1, or n-1 otherwise it can be judged not primePre =x; } if(X! =1)return false;//Fermat theorem } return true;}
In front of this algorithm is tested or relatively reliable, can be used as a template.
Efficiency, VC RELEASE mode, using three cycles m-r, test the 19,999th prime number 224729, fast division fast and test No. 20000 prime number 224737, M-r method fast
Therefore, to ensure maximum efficiency, test large number n, you can first use the first 19,999 primes for fast division to eliminate, and then use the M-r test.
Ac_von Original, reproduced please specify the source: http://www.cnblogs.com/vongang/archive/2012/03/15/2398626.html
Miller-rabin Prime number test algorithm