I read the Miller-Rabin prime number Testing Algorithm on the computation Guide several days ago. Today I Just Want To sum up and take notes.
Before the Miller-Rabin test, we first talk about two more efficient functions for finding a * B % N and AB % N. Here we use binary ideas to split B into binary values, then add (multiply) to)
// A * B % N
// Example: B = 1011101 then a * B mod n = (A * 1000000 mod n + A * 10000 mod n + A * 1000 mod n + A * 100 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;
B> = 1;
}
Return res;
}
// A ^ B % N
// Likewise
Ll mod_exp (ll a, LL B, ll N ){
Ll res = 1;
While (B ){
If (B & 1) RES = mod_mul (Res, A, N );
A = mod_mul (a, a, n );
B> = 1;
}
Return res;
}
The following describes the Miller-Rabin test:
Ferma's Theorem: For prime p and any integer a, there is AP then a (mod p) (same remainder ). In turn, P is almost certainly a prime number when AP has a (mod P.
Pseudo Prime Number: If n is a positive integer and there is a positive integer a that is equal to n that satisfies an-1 then 1 (mod N), we say n is a Pseudo Prime Number based on. If a number is a pseudo prime number, it is almost certainly a prime number.
Miller-Rabin Test: Continuously select a base B (s) not greater than n-1, calculate whether each time there is a bn-1 mod 1 (mod N), if each time is true, n is a prime number, otherwise it is a Union number.
Pseudocode:
Function Miller-Rabin (n : longint) :boolean;
begin
for i := 1 to s do
begin
a := random(n - 2) + 2;
if mod_exp(a, n-1, n) <> 1 then return false;
end;
return true;
end;
Note: The Miller-Rabin test is probabilistic, not deterministic. However, because the probability of errors after multiple runs is very small, the practical application is feasible. (The probability of a Miller-Rabin test being successful is 3/4)
The pseudo-code implementation mentioned above is very short. There is also a theorem below to improve Miller's test efficiency:
Quadratic probe Theorem:
If P is an odd prime number, the solution of X2 forward 1 (mod P) is x = 1 | x = p-1 (mod P );
We can use the quadratic probe theorem to add some details on the implementation of Miller-Rabin. The specific implementation is as follows:
Bool miller_rabin (ll n ){
If (n = 2 | n = 3 | n = 5 | n = 7 | n = 11) return true;
If (n = 1 |! (N % 2) |! (N % 3) |! (N % 5) |! (N % 7) |! (N % 11) return false;
Ll X, pre, U;
Int I, j, k = 0;
U = n-1; // x ^ U % N
While (! (U & 1) {// if u is an even number, U is shifted to the right, and K is used to record the number of shifts.
K ++; U >>= 1;
}
Srand (LL) time (0 ));
For (I = 0; I <s; ++ I) {// perform the s test
X = rand () % (n-2) + 2; // random number in [2, n)
If (X % n) = 0) continue;
X = mod_exp (x, u, n); // calculate (x ^ U) % N,
Pre = X;
For (j = 0; j <K; ++ J) {// Add the amount of the shift offset, and add a secondary test to this place.
X = mod_mul (x, x, N );
If (x = 1 & Pre! = 1 & Pre! = N-1) return false; // quadratic test theorem. Here, if X = 1, pre must be equal to 1, or n-1. Otherwise, it can be determined that it is not a prime number.
Pre = X;
}
If (X! = 1) return false; // ferma's Theorem
}
Return true;
}
This algorithm is reliable and can be used as a template. I have also found other templates for this dish, but some have tested 9 as a prime number, Khan -_-!
Ac_von original, reprinted please indicate the source: http://www.cnblogs.com/vongang/archive/2012/03/15/2398626.html