GastonFontenla

Raiz

Nov 22nd, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. BigNumber raiz(BigNumber n)
  2. {
  3.     int cantDigitos = (n.size()+1)/2;
  4.     string r = "1"+string(cantDigitos-1, '0');
  5.  
  6.     BigNumber res = r, aux("0");
  7.  
  8.     for(int i=0; i<cantDigitos; i++)
  9.     {
  10.         for(int j=(i ? '0' : '1'); j<='9'; j++)
  11.         {
  12.             r[i] = j;
  13.             aux = r;
  14.  
  15.             if(aux*aux <= n)
  16.             {
  17.                 res = aux;
  18.             }
  19.             else
  20.             {
  21.                 r[i]--;
  22.                 break;
  23.             }
  24.         }
  25.     }
  26.  
  27.     return res;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment