Prime number determination algorithm MILLER RABIN

Source: Internet
Author: User

Entry-level sieve primes-trial Division, Complexity O (n^2)

BOOL Long Long N) {    for (longlong2; I <= sqrt (n); i++)    if 0 return false ;     return true ;}

After learning a period of time algorithm, you should understand the Sieve method to calculate the prime number, the complexity is slightly higher than O (n)

#include <iostream>#include<cstring>using namespacestd;Const intMAXN =500000;BOOLISP[MAXN];intP[MAXN];intCNT =0;//Preserve The number of vegetarian charactersvoidGetp () { for(inti =1; i < MAXN; i++) Isp[i] =1;//Default total number of primes     for(inti =2; i < MAXN; i++){        if(!isp[i])Continue;//if it's not prime, continue it off .p[cnt++] =i;  for(intj = i *2; J < MX; J + = i) isp[j] =0;//record, roll back processing    }}intMain () {GETP ();  for(inti =0; I < CNT; i++) printf ("%d", P[i]); return 0; }

Of course, it is not difficult to find that if max value is too large, not only the space will explode, and the beginning of the sweep is very metaphysical, is not advisable.

Then the Miller Rabin algorithm was introduced. It is possible to determine whether a large number is prime, but it is not guaranteed to be correct. We can only make this error a small probability by executing the algorithm multiple times. Fortunately, unless you are a non-king, this algorithm is generally not a problem.

The theoretical basis of the algorithm: 1. Fermat theorem: If n is an odd prime, A is any positive integer (1≤a≤n?1), then a^ (n-1) ≡1 mod n. 2. Deduce the self-Fermat theorem (the specific process I did not understand, Orz), if n is an odd prime, the n?1 is expressed in the form of 2^s*r, R is odd, A and n is any random integer of the mutual elements, then a^r≡1 mod n or to a J (0≤j≤s?1, j∈z) equation a^ (2j R) ≡?1 MoD n was established. The wise reader has found that the above theorem is a sine qua non sufficient condition for a number, so this is the imprecise original of the algorithm, and for some specific test operator A, there are some composite that satisfy the above theorem. So we have to find a few more tests, which can make the error rate greatly reduced. Then we follow the above Theorem 2, first repeat N times experiment. For each experiment, a random check operator A, with the theorem 2 to check to see if the operator a, n can meet a^r≡1 mod n or a J (0≤j≤s?1, j∈z) equation a^ (2JR) ≡?1 mod n * * If any experiment is not satisfied, then determine Not a prime number, if satisfied, can be approximated as a prime number (the error rate is very small).
#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<map>using namespacestd;Const intTimes = -;intNumber =0; Map<Long Long,int>m;Long LongRandom (Long LongN)//generates a random number of [0, N]{    return((Double) rand ()/Rand_max*n +0.5);}Long LongQ_mul (Long LongALong LongBLong LongMoD)//fast calculation (a*b)% mod{    Long LongAns =0;  while(b) {if(B &1) {b--; Ans= (ans+ a)%MoD; } b/=2; A= (A + a)%MoD; }    returnans;}Long LongQ_pow (Long LongALong LongBLong LongMoD)//fast calculation (a^b)% mod{    Long LongAns =1;  while(b) {if(B &1) {ans=Q_mul (ans, A, mod); } b/=2; A=Q_mul (A, A, mod); }    returnans;}BOOLWitnessLong LongALong LongN)//the essence of the Miller_rabin algorithm{//Test operator A to verify n is not a prime number    Long LongTEM = N-1; intj =0;  while(Tem%2==0) {tem/=2; J++; }    //split the n-1 into A^r * s    Long Longx = Q_pow (A, TEM, N);//get a^r mod n    if(x = =1|| x = = N-1)return true;//The remainder of 1 is the prime number.     while(j--)//otherwise the test condition 2 see if there is a satisfying J{x=Q_mul (x, x, N); if(x = = N-1)return true; }    return false;}BOOLMiller_rabin (Long LongN)//checks if n is a prime number{    if(n = =2)        return true; if(N <2|| N%2==0)        return false;//If 2 is a prime number, if <2 or >2 is not a prime number     for(inti =1; I <= times; i++)//do the Times random test    {        Long LongA = Random (n-2) +1;//get the random test operator a        if(!witness (A, N))//use a To test whether N is a prime number            return false; }    return true;}intMain () {Long Longtar;  while(Cin >>tar) {        if(Miller_rabin (TAR))//Check if tar is primecout <<"Yes, prime!"<<Endl; Elsecout<<"No, not prime."<<Endl; }    return 0;}

Prime number determination algorithm MILLER RABIN

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.