Large prime number judgment and prime factor decomposition (Miller-rabin,pollard_rho algorithm)

Source: Internet
Author: User
Tags gcd

#include <stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>#include<iostream>#include<algorithm>using namespacestd;//****************************************************************//Miller_rabin Algorithm for prime number testing//fast, and can judge the number of <2^63//****************************************************************Const ints= -;//the number of random algorithms, the larger the s, the smaller the probability of the wrong judgment//calculates (a*b)%c. A, B is a number of long long, directly multiplying the possible overflow//a,b,c <2^63Long LongMult_mod (Long LongALong LongBLong Longc) {a%=B; b%=C; Long Longret=0;  while(b) {if(b&1) {ret+=a;ret%=C;} A<<=1; if(a>=c) a%=C; b>>=1; }    returnret;}//Calculate x^n%cLong LongPow_mod (Long LongXLong LongNLong LongMoD//x^n%c{    if(n==1)returnX%MoD; X%=MoD; Long Longtmp=x; Long Longret=1;  while(n) {if(n&1) ret=Mult_mod (RET,TMP,MOD); TMP=Mult_mod (TMP,TMP,MOD); N>>=1; }    returnret;}//Based on A, n-1=x*2^t a^ (n-1) =1 (mod n) verifies that n is not composite//must be composite returns true, not necessarily returning falseBOOLCheckLong LongALong LongNLong LongXLong Longt) {    Long Longret=Pow_mod (a,x,n); Long Longlast=ret;  for(intI=1; i<=t;i++) {ret=Mult_mod (ret,ret,n); if(ret==1&&last!=1&&last!=n-1)return true;//Compositelast=ret; }    if(ret!=1)return true; return false;}//Miller_rabin () algorithm prime number determination//is a prime number that returns True. (May be pseudo prime, but with minimal probability)//composite returns false;BOOLMiller_rabin (Long LongN) {    if(n<2)return false; if(n==2)return true; if((n&1)==0)return false;//even    Long Longx=n-1; Long Longt=0;  while((x&1)==0) {x>>=1; t++;}  for(intI=0; i<s;i++)    {        Long LongA=rand ()% (n1)+1;//rand () requires Stdlib.h header file        if(check (a,n,x,t))return false;//Composite    }    return true;}//************************************************//Pollard_rho algorithm for mass factor decomposition//************************************************Long Longfactor[ -];//mass factor decomposition results (unordered at first return)intTol//the number of factorization. Array of small labels starting from 0Long LonggcdLong LongALong Longb) {    if(a==0)return 1;//???????    if(a<0)returnGCD (-b);  while(b) {Long Longt=a%b; A=b; b=T; }    returnA;}Long LongPollard_rho (Long LongXLong Longc) {    Long LongI=1, k=2; Long LongX0=rand ()%x; Long Longy=x0;  while(1) {i++; X0= (Mult_mod (x0,x0,x) +c)%x; Long LongD=GCD (yx0,x); if(d!=1&AMP;&AMP;D!=X)returnD; if(y==x0)returnx; if(i==k) {y=x0;k+=K;} }}//factor decomposition of nvoidFINDFAC (Long LongN) {    if(Miller_rabin (n))//Prime Number{Factor[tol++]=N; return; }    Long Longp=N;  while(p>=n) P=pollard_rho (P,rand ()% (N-1)+1);    FINDFAC (P); FINDFAC (n/p);}intMain () {//Srand (Time (NULL));//need to time.h header file//POJ on g++ can't add this sentence    Long LongN;  while(SCANF ("%i64d", &n)! =EOF) {Tol=0;        FINDFAC (n);  for(intI=0; i<tol;i++) printf ("%i64d", Factor[i]); printf ("\ n"); if(Miller_rabin (n)) printf ("yes\n"); Elseprintf"no\n"); }    return 0;}

Large prime number judgment and prime factor decomposition (Miller-rabin,pollard_rho 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.