Integer decomposition of Trial Division
int factor[11000]; recording factor int ct; Number of recording elements void Divide (int N) { ct = 0; for (int i = 2; I <= sqrt (n*1.0), ++i) {while (N% i = = 0) { factor[ct++] = i; N/= i; } } if (n! = 1) factor[ct++] = N;}
Integer decomposition by sieve method
const int MAXN = 11000;int prime[maxn],nprime,ct; Prime[] Storage Prime, nprime for the number of elements, CT is the number of prime factors bool Isprime[maxn];int factor[11000];void getprime () { nprime = 0; for (int i = 2; I <= maxn; ++i) isprime[i] = 1; for (int i = 2; I <= sqrt (MAXN-1.0); ++i) { if (Isprime[i]) { prime[nprime++] = i; for (int j = i*i; j < Maxn; j+=i) { isprime[j] = 0; } } } void Divide (int N) { int temp = sqrt (n*1.0); ct = 0; for (int i = 0; i < nprime; ++i) { if (Prime[i] > Temp) break ; while (n%prime[i] = = 0) { factor[ct++] = prime[i]; N/= prime[i]; } } if (n! = 1) factor[ct++] = N;}
Pollardrho Large integer decomposition
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #define Max_val (POW ( 2.0,60))//miller_rabbin primality test//__int64 mod_mul (__int64 x,__int64 y,__int64 mo)//{//__int64 t;//x%= mo;//for (t = 0; Y x = (x<<1)%mo,y>>=1)//if (Y & 1)//T = (t+x)%mo;////return T;//}__int64 Mod_mul (__i Nt64 x,__int64 y,__int64 mo)//x * y% mo{__int64 T,t,a,b,c,d,e,f,g,h,v,ans; T = (__int64) (sqrt (double (MO) +0.5)); t = T*t-mo; A = x/t; b = x% T; c = y/t; d = y% T; e = a*c/t; f = a*c% T; v = ((a*d+b*c)%mo + e*t)% Mo; g = v/t; h = v% T; Ans = (((f+g) *t%mo + b*d)% mo + h*t)%mo; while (ans < 0) ans + = mo; return ans;} __int64 mod_exp (__int64 num,__int64 t,__int64 mo)//num^t% mo{__int64 ret = 1, temp = num% mo; for (; t; t >>=1,temp=mod_mul (Temp,temp,mo)) if (T & 1) ret = Mod_mul (RET,TEMP,MO); return ret;} BOOL Miller_rabbin (__int64 N)//millerrabbin prime Test {if (n = = 2) return true; if (N < 2 | |! N&1)) return false; int t = 0; __int64 a,x,y,u = n-1; while ((u & 1) = = 0) {t++; U >>= 1; } for (int i = 0; i < i++) {a = rand ()% (n-1) +1; x = Mod_exp (a,u,n); for (int j = 0; J < T; j + +) {y = Mod_mul (x,x,n); if (y = = 1 && x! = 1 && x! = n-1) return false; x = y; } if (x! = 1) return false; } return true; Pollarrho large integer factor decomposition __int64 minfactor; The smallest element factor __int64 gcd (__int64 A,__int64 b) {if (b = = 0) return A; return gcd (b, a% b);} __int64 Pollarrho (__int64 n, int c) {int i = 1; Srand (Time (NULL)); __int64 x = rand ()% n; __int64 y = x; int k = 2; while (true) {i++; x = (Mod_exp (x,2,n) + c)% n; __int64 d = gcd (y-x,n); if (1 < d && D < n) rEturn D; if (y = = x) return n; if (i = = k) {y = x; K *= 2; }}}void Getsmallest (__int64 n, int c)//Split n{if (n = = 1) return; if (Miller_rabbin (n))//If n is prime number {if (n < minfactor) Minfactor = n; Return }//n is not a prime __int64 val = n; while (val = = N) val = Pollarrho (n,c--); Get the approximate Val getsmallest (val,c); Attempt to split the constraint of n Val getsmallest (n/val,c); Try splitting another approximate n/val of n}
Integer decomposition by division integer decomposition pollardrho Large integer decomposition "template"