Miller_Rabbin algorithm determines large prime numbers

Ordinary prime number test we have O (√ n) In addition to the test algorithm. In fact, we have O (s * log³n) algorithm.

 

Miller_Rabbin algorithm to introduce the following idea:

A Theorem: If p is a prime number, and (a, p) = 1, then a ^ (p-1) ≡1 (mod p). That is, if p is a prime number, and a, p prime, then a is (p-1) th power is divided by a constant p is equal to 1. (Fermat's little theorem)

Theorem 2: If p is a prime number, then for x (0 <x <p), if x ^ 2 mod p is equal to 1, then x = 1 or p-1.

 

It utilizes Fermat's little theorem, that is: if p is a prime number, and a, p prime, then a ^ (p-1) mod p is equal to 1 constant. I.e. for all positive integers less than p, it should be a compound a ^ (p-1) mod p is equal to 1 constant. So according to whether the reverse proposition, for a p, as long as we include a a (a <p) does not comply with this identity (that is, a ^ (p-1) mod p identically equal to 1 formula) it can be determined that p is not a prime number. Miller-rabin algorithm is used many times to try different if p is a prime number.

 

However, every attempt was also made during an optimization operation, with a small amount to increase the probability of a detected p is not a prime number. This optimization is called secondary detection. It is based on a theorem: If p is a prime number, then for x (0 <x <p) , if x ^ 2 mod p is equal to 1, then x = 1 or p-1. Inverse Proposition NO: if for x (0 <x <p) , if x ^ 2 mod p is not equal to 1, then p is not prime. According to this theorem, we have to calculate a ^ (p-1) mod p is equal to 1, can be calculated, setting = 1-P (T ^ 2) * K . We begin with a ^ k, will continue its square until a ^ (p-1), if it is found after a particular square mod p is equal to 1, then no explanation consistent with the inverse proposition conditions of use quadratic probing theorem, immediately check x is equal to 1 or p-1, can not be directly determined if p is a composite number.

 

some problems:

1, for p-1 = (2 ^ t ) * k the t search method, may be recorded at there end of the x (x indicates (p-1)) in binary form the number 0, such as binary (1000) B represented decimal (8) D, the binary number 8 at the end of 0 is 3, it is 8 = (2 ^ 3) * 1; another example, a binary (11000) B represents the decimal (24) D, 24 of there are three lower end of the 0 form of a binary number, it is 24 = (2 ^ 3) * 3

 

2, for the random number a is generated because of our algorithm determines that p the number is not a prime number, the prime numbers and any numbers are coprime subject have required a <p so it generates a random [1, p- 1] on the line number in the range

 

3, the complexity of the algorithm is O (s * log³n), this is s good that we set in advance, as said above, we are using Fermat's Little Theorem whether the inverse proposition, but the converse proposition of the theorem is not necessarily founded but gratifying is that in most cases is established. So we can find a few to test whether a number is prime p, find a few numbers, this number is much s

 

We can prove, after more than two theorems, test probability of error s times up to 2 ^ (- s), so this algorithm is very reliable.

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<map>
 7 #include<vector>
 8 #include<math.h>
 9 #define mem(a,x) memset(a,x,sizeof(a))
10 using namespace std;
11 typedef long long LL;
12 const int maxn=50005;
13 const int mod=26;
14 const  int INF = 0x3f3f3f3f ;
 15  const  int Times = 10 ;
 16  const  int N = 5500 ;
 . 17  LL CT, CNT;
 18 is  LL FAC [N], NUM [N];
 . 19 LL GCD (A LL, LL B)   // seeking the greatest common divisor of two numbers 
20 is  {
 21 is      return B GCD (B, A%? B): A;
 22 is  }
 23 is LL Multi (LL A, B LL, LL m)   // quickly by 
24  {
 25      LL ANS = 0 ;
 26 is      A% = m;
27     while(b)
28     {
29         if(b & 1)
30         {
31             ans = (ans + a) % m;
32             b--;
33         }
34         b >>= 1;
35         a = (a + a) % m;
36     }
37     return ans;
38 }
39 LL pow(LL a, LL b, LL m)  //快速幂
40 {
41     LL ans = 1;
42     a %= m;
43     while(b)
44     {
45         if(b & 1)
46         {
47             ans = multi(ans, a, m);
48             b--;
49         }
50         b >>= 1;
51         a = multi(a, a, m);
52     }
53     return ans;
54 }
55 bool Miller_Rabin(LL n)  //判断是不是素数
56 {
57     if(n == 2) return  to true ;
 58      IF (n-< 2 ! || (& n- . 1 )) return  to false ;
 59      LL = n-m - . 1 ;
 60      int K = 0 ;
 61 is      the while ((m & . 1 ) == 0 )
 62 is      {
 63 is          ++ k;   // this is k when we speak of T 
64          m >> = . 1 ;   // this is a m k 
65      }
 66      for ( int I = 0; I <Times; I ++) // Times is s (looking for a number) we previously defined 
67      {
 68          LL a = RAND ()% (n-- . 1 ) + . 1 ;   // find a [1, n any number -1] in 
69          LL X = POW (a, m, n-);
 70          LL Y = 0 ;
 71 is          for ( int J = 0 ; J <K; J ++ )
 72          {
 73 is              Y = Multi (X, X , n-);
 74              IF (Y == . 1 ! && X = . 1 ! && n-X = - . 1 ) return  to false;
 75              X = Y;
 76          }
 77          IF (Y =! . 1 ) return  to false ;
 78      }
 79      return  to true ;
 80  }
 81  int main ()
 82  {
 83      LL n-;
 84      the while (CIN >> n-)
 85      {
 86          IF (Miller_Rabin (n-))
 87              the printf ( " is a prime number \ n " );
 88          the else the printf ( " not a prime number \ n");
89     }
90     return 0;
91 }

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/12667418.html