Guest User

Untitled

a guest
Dec 17th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. var BigInt = require('big-integer');
  2.  
  3. function sqrt(c) {
  4. if (c.isZero()) {
  5. return c;
  6. }
  7. if (c.lesser(4)) {
  8. return BigInt(1);
  9. }
  10.  
  11. var n, p;
  12. var high = c.shiftRight(1);
  13. var low = BigInt.zero;
  14.  
  15. while (high.greater(low.add(1))) {
  16. n = high.add(low).shiftRight(1);
  17. p = n.square();
  18. if (c.lesser(p)) {
  19. high = n;
  20. } else if (c.greater(p)) {
  21. low = n.equals(1) ? n.add(1) : n;
  22. } else {
  23. break;
  24. }
  25. }
  26. if (c.equals(p)) {
  27. return n;
  28. } else if (c.equals(high.square())) {
  29. return high;
  30. }
  31. return low;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment