Miller_rabin prime number test algorithm template comparison

Source: Internet
Author: User
Tags square root

Yesterday in Usaco did a judge of the problem, just want to learn about the Miller_rabin prime number test algorithm, find two templates on the web, the first is very concise, running speed is very fast, but will be judged a few non-prime number of the wrong; the second kind of trouble, the speed is very slow, So I would like to find the first template can not judge the non-prime sentence, the results of a day, the computer only found 10^8 below, 10^9 inside there are 2 not found, but the correct template running speed is too slow, my computer is too slag, can not afford the time, let alone, After that, there is a better way to understand and update.


The first type: from the Jilin University ACM template

Just started with a random number test, I think I have learned that as long as the 2,7,61 can judge the number of unsigned, then made a change, handed the Usaco, because the special sentenced 5,7,11, unexpectedly water, and then happily to the end ...

Later to do another prime number problem, finally exposed its shortcomings, but feel it to judge quickly, and only very little can not judge, then find out its special case, after a day of boring enumeration finally found.

Text contrast or write their own fast, the net under the results of memory explosion and run slowly, write their own 10s results (50MB of text).

at present only for 2,3,5, find the special case is: 4097,1048577,16777217,25326001, (960946321 is a special case in 10^9, this algorithm enumerates 10^9, my slag computer is about 8min, Don't say the tighter one.)

BOOL Witness (long long A,long long n) {    long long d=1,x;    int I=ceil (log (n-1.0)/log (2.0)) -1;//ceil () is rounded up for    (; i>=0;i--) {        x=d;        D= (d*d)%n;        if (d==1&&x!=1&&x!=n-1)            return true;        if ((N-1 & (1<<i)) >0)            d= (d*a)%n;    }    return d!=1;} BOOL Miller_rabin (long long N) {    int s[]={2,3,5};//can correctly judge all the primes within 10^8 by using only 2,3,5 and the following special case, enumerating over 40s    if (n==2| | n==3| | n==5| | n==7)        return true;    if (n==1| | (n&1) ==0| | 0==n%3| | 0==n%5| | n==4097| | n==1048577| | n==16777217| | N==25326001)        return false;    for (int i=0;i<3;i++)        if (Witness (S[i], n))            return false;    return true;}

The second template: most templates available online (template modified from: http://blog.csdn.net/z690933166/article/details/9860937)

Even if only with 2,7,61 judge, also odd slow, enumerate once 10^8 to run around 17min, but unsigned int no exception

typedef unsigned long long LL; ll Modular_multi (ll x,ll y,ll mo) {ll t;x%=mo;for (t=0;y;x= (x<<1)%mo,y>>=1) if (y&1) t= (t+x)%mo;return t;} ll Modular_exp (ll num,ll t,ll mo) {ll ret=1,temp=num%mo;for (; T;t>>=1,temp=modular_multi (Temp,temp,mo)) if (t &1) Ret=modular_multi (RET,TEMP,MO); return ret;} BOOL Miller_rabin (LL N) {if (n==2| | n==7| | n==61) return True;if (n==1| | (n&1) ==0) return False;int t=0,num[3]={2,7,61};//2,7,61 all the numbers within the unsigned int are sufficient, the smallest can not be judged by 4 759 123 141; 2,3,7,61 in The only number that cannot be judged in 10^16 is 856 248 225 981LL a,x,y,u=n-1;while ((u&1) ==0) t++,u>>=1;for (int i=0;i<3;i++) {A=nu M[i];x=modular_exp (A,u,n); for (int j=0;j<t;j++) {y=modular_multi (x,x,n); if (y==1&&x!=1&&x!=n-1)            return false;            Where the theorem is used, if there is 1 non-trivial square root of modulo n, then n is composite. If a number x satisfies the equation x^2≡1 (mod n), but X is not equal to modulo n for 1 of the two ' trivial ' square roots: 1 or-1, then x is the non-trivial square root of 1 for modulo n x=y;} if (x!=1)///According to Fermat theorem, if n is prime, there is a^ (n-1) ≡1 (mod n). So n cannot be a prime return false; return true;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Miller_rabin prime number test algorithm template comparison

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.