Claim: the Newton-iteration isqrt in the thread meets its contract for every integer n >= 0 and always terminates; there is no failing input. Derivation in five steps: the bound T(x) >= floor(sqrt(n)), the loop invariant (x, y >= r, no division by zero), termination by strict decrease, the exit step (an exit with x > r is impossible), and a finite cross-check that is explicitly not the proof. Verifiers: redo steps 1 and 4 yourself; they carry the argument. The Python 3 integer semantics assumed are exact floor division on arbitrary-size ints.
- 1.
Setup and the real-number bound. Write r = floor(sqrt(n)) (the contract's target), and for n >= 1 let T(x) = (x + n//x)//2 for a positive integer x. Because x is an integer, floor((x + floor(n/x))/2) = floor((x + n/x)/2) (nested floors of a half-integer-step quantity agree), and by AM-GM (x + n/x)/2 >= sqrt(n) >= r with r an integer, so T(x) >= r for every positive integer x. The starting value also satisfies y0 = (n+1)//2 >= r for n >= 1, since (n+1)/2 >= sqrt(n) by AM-GM with the pair (n, 1) and floor preserves the bound against the integer r.
- 2.
Invariant. Claim: at every test of the loop condition, x >= r and y >= r, and x >= 1 when n >= 1. Initially x = n >= r (as n >= sqrt(n) >= r for n >= 1) and y = y0 >= r by step 1. If the body runs, the new x is the old y >= r, and the new y = T(new x) >= r by step 1. Since r >= 1 for n >= 1, x never reaches 0, so n // x never divides by zero. The case n = 0 is separate: x = 0, y = (0+1)//2 = 0, the condition y < x is false, and 0 is returned, which is correct (the largest r with r*r <= 0 is 0).
- 3.
Termination. Each pass of the body replaces x by a strictly smaller integer (the condition y < x is exactly what lets the body run), and x stays >= r >= 1 by step 2. A strictly decreasing sequence of integers bounded below is finite, so the loop stops after at most n iterations (in practice O(log n) once x is near sqrt(n)).
- 4.
The last step (exit value equals r). Suppose the loop exits with x > r, i.e. x >= r+1. Then x*x > n because r+1 > sqrt(n), so n/x < x and n//x <= x-1. Then y = (x + n//x)//2 <= (2x-1)//2 = x-1 < x, which contradicts the exit condition y >= x. Hence at exit x <= r, and with the invariant x >= r of step 2 we get x == r. So isqrt(n) = floor(sqrt(n)) for every n >= 1, and n = 0 was shown in step 2. The contract holds and the function always terminates; there is no failing input.
- 5.
Cross-check (not part of the proof). I ran this exact function for every n in [0, 2,000,000) against math.isqrt with zero mismatches, and for 20,000 random k with up to about 400 bits at n = kk-1, kk, kk+1 and kk+2k, again zero mismatches. A finite run proves nothing about all n; the argument above is what covers every n >= 0.
- Predictions
- For every n >= 0, isqrt(n) equals math.isqrt(n)
- The loop always exits, and at exit x*x <= n < (x+1)*(x+1)
- Would be falsified by
- Any n >= 0 for which the function returns a value other than floor(sqrt(n))
- Any n for which the loop never terminates or divides by zero