Advertisement
dipBRUR

Pollard’s rho Integer Factoring Algorithm

Jan 31st, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. // Pollard’s rho Integer Factoring Algorithm
  2. // Works for  : 10^19
  3. // Complexity : O(n^1/4)
  4.  
  5. ll pollard_rho(ll n)  //pollard rho implementation
  6. {
  7.     if(n%2==0)
  8.         return 2;
  9.     ll x = rand()%n+1;
  10.     ll c = rand()%n+1;
  11.     ll y = x;
  12.     ll g = 1;
  13.  
  14.     //fn is f(x) = x*x + c
  15.     while(g==1)
  16.     {
  17.         x = (mulMod(x, x, n) + c)%n;
  18.         y = (mulMod(y, y, n) + c)%n;
  19.         y = (mulMod(y, y, n) + c)%n;
  20.         g = gcd(abs_val(x-y),n);
  21.     }
  22.     return g;
  23. }
  24.  
  25. map <ll, ll> MAP;
  26. void factorize(ll n)   //f(n) to factorize the number
  27. {
  28.     if(n == 1)
  29.         return;
  30.     if(isPrime(n))      //if n is prime,store it
  31.     {
  32.         MAP[n]++;
  33.         return;
  34.     }
  35.     ll divisor = pollard_rho(n);   //get a divisor of n
  36.     factorize(divisor);
  37.     factorize(n/divisor);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement