sqrt function

BigInt sqrt(
  1. BigInt value
)

Computes round(sqrt(value)) @param value the value for which to compute the square root, rounded down

Implementation

BigInt sqrt(BigInt value) {
  invariant(value >= BigInt.zero, 'NEGATIVE');

  if (value < MAX_SAFE_INTEGER) {
    return BigInt.from(math.sqrt(value.toInt()).round());
  }

  BigInt z = value;
  BigInt x = BigInt.from(value/ TWO) + ONE;
  while (x < z){
    z = x;
    x = BigInt.from((BigInt.from(value/ x) + x)/ TWO);
  }
  return z;
}