document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.     @Author  : Joel Fernandez
  3.     @web     : Codebotic.blogspot.com
  4.     @Fecha   : 30/06/2015
  5.     @Tema    : Implementacion del Algoritmo Rho Pollard
  6.                (Factorizacion de numeros enteros)
  7. */
  8.  
  9. #include<iostream>
  10. #include<cstdlib>
  11.  
  12. using namespace std;
  13.  
  14. /* Funcion que saca el valor absoluto a
  15.    un numero */
  16. long long int absoluto( long long int n)
  17. {
  18.     if(n>=0)
  19.     {
  20.         return n;
  21.     }
  22.     else
  23.     {
  24.         n = -n;
  25.         return n;
  26.     }
  27. }
  28.  
  29. /* Funcion que saca el Maximo Comun
  30.    Divisor por Euclides a un numero */
  31. long long int mcd(long long int a,long long int b )
  32. {
  33.     if(b==0)
  34.         return a;
  35.     else
  36.         mcd(b,a%b);
  37. }
  38.  
  39. /* Algoritmo Rho Pollard */
  40. int rho_pollard(int n)
  41. {
  42.     long long int a,b,d,e;
  43.     int i;
  44.     bool band;
  45.  
  46.     a=2;
  47.     b=2;
  48.     i = 1;
  49.     band= false;
  50.  
  51.     while(band==false)
  52.     {
  53.         a = (a*a+1)%n;
  54.         b = (b*b+1)%n;
  55.         b = (b*b+1)%n;
  56.         e=absoluto(a-b);
  57.         d= mcd(e,n);
  58.  
  59.         cout<<"\\n\\t i = "<<i;
  60.         cout<<"\\n\\t\\t a: "<<a;
  61.         cout<<"\\n\\t\\t b: "<<b;
  62.         cout<<"\\n\\t\\t d = mcd("<<e<<","<<n<<"):"<<d;
  63.  
  64.         if(1<d&&d<n)
  65.         {
  66.             band = true;
  67.             return d;
  68.         }
  69.         if(d>=n)
  70.         {
  71.             band = true;
  72.             return -1;
  73.         }
  74.  
  75.         i++;
  76.     }
  77. }
  78.  
  79. int main(void)
  80. {
  81.     system("color 0a");
  82.     int x,n;
  83.  
  84.     cout<<"\\t    ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"<<endl;
  85.     cout<<"\\t    º                                                         º"<<endl;
  86.     cout<<"\\t    º                  Algoritmo Rho de Pollard               º"<<endl;
  87.     cout<<"\\t    º                                                         º"<<endl;
  88.     cout<<"\\t    ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ  "<<endl;
  89.     cout<<endl<<endl;
  90.     cout<<"\\n\\tIngrese un numero: ";
  91.     cin>>n;
  92.  
  93.     x = rho_pollard(n);
  94.  
  95.     if(x!=-1)
  96.     {
  97.         cout<<"\\n\\n\\t El Primer Factor es: "<<x;
  98.         cout<<"\\n\\t El Segundo Factor es:"<<n/x;
  99.         cout<<endl;
  100.     }
  101.     else
  102.     {
  103.         cout<<"\\n\\tNumero no Factorizable...";
  104.         cout<<endl;
  105.     }
  106.  
  107.     return 0;
  108. }
');