10^9 above prime number determination, Miller_rabin algorithm

Source: Internet
Author: User

#include <iostream> #include <cstdio> #include <ctime> #include <string.h> #include < Stdlib.h> #define LL Long longusing namespace Std;const int s=20;//random algorithm judgment number, S larger, the smaller the probability of error will ans;//given a number, judging whether it is a prime (commonly used long    Long large number) ll Mult_mod (ll a,ll b,ll MoD)//(A*B)%c a,b,c<2^63{a%=mod;    B%=mod;    LL ans=0;            while (b) {if (b&1) {ans=ans+a;        if (ans>=mod) Ans=ans-mod;        } a=a<<1;        if (a>=mod) A=a-mod;    b=b>>1; } return ans;    ll Pow_mod (ll a,ll b,ll MoD)//a^b%mod{LL Ans=1;    A=a%mod;        while (b) {if (b&1) {ans=mult_mod (ans,a,mod);        } a=mult_mod (A,a,mod);    b=b>>1; } return ans; Based on A, n-1=x*2^t a^ (n-1) =1 (mod n) verifies that n is composite//must be composite return true, not necessarily return Falsebool check (ll a,ll n,ll X,ll t) {ll ret=pow_mod    (A,x,n);    LL Last=ret;        for (int i=1;i<=t;i++) {ret=mult_mod (ret,ret,n); if (Ret==1 &&    Last!=1 && last!=n-1) return true;//composite Last=ret;    } if (ret!=1) return true; else return false;} Miller_rabin () algorithm prime number determination//is a prime number to return true.    (may be a pseudo prime, but the probability is very small)//Composite Returns False;bool Miller_rabin (Long long N) {if (n<2) return false;    if (n==2) return true;    if ((n&1) ==0) return false;//even LL x=n-1;    LL t=0;    while ((x&1) ==0) {x>>=1;t++;} for (int i=0;i<s;i++) {LL a=rand ()% (n-1) +1;//rand () requires Stdlib.h header file if (check (a,n,x,t)) return Fals e;//Composite} return true;    void find (Long long n,int c) {if (n==1) return;        if (Miller_rabin (n)) {//m[n]++;        Ans=min (Ans,n);    return;    } Long long p=n;    while (p>=n) P=pollard_rho (p,c--);    Find (P,C); Find (n/p,c);}   int main () {int A;   scanf ("%d", &a);       while (a--) {long long x;       scanf ("%lld", &x);       if (Miller_rabin (x)) cout<< "Prime" <<endl;        else{find (x,12312); Cout<<ans<<endL }   }}

  

10^9 above prime number determination, Miller_rabin algorithm

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.