Guest User

Untitled

a guest
Oct 15th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. int isqrt(unsigned x) {
  2.   unsigned x1;
  3.   int s, g0, g1;
  4.  
  5.   if (x <= 1) return x;
  6.  
  7.   s = 1;
  8.   x1 = x - 1;
  9.  
  10.   if (x1 > 65535) {s = s + 8; x1 = x1 >> 16;}
  11.  
  12.   if (x1 > 255) {s = s + 4; x1 = x1 >> 8;}
  13.  
  14.   if (x1 > 15) {s = s + 2; x1 = x1 >> 4;}
  15.  
  16.   if (x1 > 3) {s = s + 1;}
  17.  
  18.   g0 = 1 << s;
  19.  
  20.   g1 = (g0 + (x >> s)) >> 1;
  21.  
  22.   while (g1 < g0) {
  23.     g0 = g1;
  24.     g1 = (g0 + (x/g0)) >> 1;
  25.   }
  26.  
  27.   return g0;
  28. }
Add Comment
Please, Sign In to add comment