Find a root of by repeatedly following the tangent line:

Quadratic convergence — the number of correct digits roughly doubles each step, so ~6 iterations take you from 1 to 64 correct bits.

double newton(function<double(double)> f, function<double(double)> df, double x0) {
    double x = x0;
    for (int it = 0; it < 100; it++) {
        double fx = f(x);
        if (fabs(fx) < 1e-15) break;
        x -= fx / df(x);
    }
    return x;
}

Integer square root — the practical case

long long isqrt(long long n) {
    if (n < 0) return -1;
    long long x = (long long)sqrtl((long double)n);   // good initial guess
    while (x * x > n) x--;                             // correct for rounding
    while ((x + 1) * (x + 1) <= n) x++;
    return x;
}

Always correct the result of sqrt. sqrt(n) on a double is inexact for near and returns a value off by one surprisingly often — a classic source of wrong answers. The two adjustment loops cost nothing and make it exact.

The same pattern works for integer -th roots:

long long iroot(long long n, int k) {
    long long x = (long long)powl((long double)n, 1.0L / k);
    while (ipow(x + 1, k) <= n) x++;
    while (x > 0 && ipow(x, k) > n) x--;
    return x;
}

Convergence and its failures

Newton converges quadratically near a simple root with . It fails when:

FailureSymptomFix
huge jump, divergencebisection fallback
Bad starting pointconverges to a different root, or divergesbracket first
Multiple root ( at the root)linear, not quadratic, convergenceuse for multiplicity
Oscillationcycles between two pointsdamping, or a hybrid method

The robust choice is a hybrid: maintain a bracketing interval, take a Newton step, and fall back to bisection whenever the step leaves the bracket. That is what Brent's method does, and it guarantees convergence while keeping Newton’s speed.

For contest purposes, binary search with a fixed iteration count is usually the right call: slower per step, but unconditionally correct and impossible to get wrong.

Newton beyond real numbers

The same iteration works in any setting with the right notion of “derivative”, and each application doubles the precision:

SettingIterationDoubles
Realscorrect digits
Power series inversecorrect coefficients
Power series , , analogouscoefficients
$p$-adic (Hensel lifting)the power of
Matrix inversecorrect digits
Big-integer reciprocalbits — this is how division is implemented

Recognising Newton in all of these is genuinely useful: it turns “how do I compute the inverse of a power series in ?” into a solved problem.

Because the cost of the last doubling step dominates, the total is — the same as a single multiplication.

Other root-finding methods

MethodConvergenceNeeds
Bisectionlinear (1 bit/step)no — always works
Newtonquadraticyes
Secantsuperlinear ()no
Regula falsilinear to superlinearno
Brentsuperlinear, guaranteedno
Fixed-point iterationdepends on the mapno

The secant method replaces with a finite difference — useful when the derivative is unavailable, at a modest cost in convergence rate.

Optimisation via Newton

Applying Newton to finds a stationary point:

Quadratically convergent, but it finds any critical point, not necessarily a minimum. Ternary search is safer for unimodal functions in a contest.

See also: Binary Search · Formal Power Series · Hensel Lifting