admath.cpp 341 B

123456789101112131415161718192021
  1. #include "admath.h"
  2. int sqrt(int a)
  3. {
  4. int loc =128;
  5. int x = loc * loc;
  6. int level = 6;
  7. while((level >= 0) && (x != a))
  8. {
  9. if(a > x) loc += (1<<level);
  10. else loc -= (1<<level);
  11. level --;
  12. x = loc * loc;
  13. }
  14. int test = (loc * loc)-a;
  15. if (test >= loc) loc --;
  16. else if (test < (loc * -1)) loc ++;
  17. return loc;
  18. }