double sqrt_newton(double n) { const double eps = 1E-15; double x = 1; while (true) { double nx = (x + n / x) / 2; if (abs(x - nx) < eps) break; x = nx; } return x; }
int isqrt_newton(int n) { int x = 1; bool decreased = false; for (;;) { int nx = (x + n / x) >> 1; if (x == nx || (nx > x && decreased)) break; decreased = nx < x; x = nx; } return x; }
public static BigInteger isqrtNewton(BigInteger n) { BigInteger a = BigInteger.ONE.shiftLeft(n.bitLength() / 2); boolean p_dec = false; for (;;) { BigInteger b = n.divide(a).add(a).shiftRight(1); if (a.compareTo(b) == 0 || a.compareTo(b) < 0 && p_dec) break; p_dec = a.compareTo(b) > 0; a = b; } return a; }