(s <= 1) return s; // Initial estimate (must be too high) unsigned int x0 = s / 2; // Update unsigned int x1 = (x0 + s / x0) / 2; while (x1 < x0) // Bound check { x0 = x1; x1 = (x0 + s / x0) / 2; } return x0; } Numerical example For example, if one computes the integer square root of using the algorithm above, one obtains the sequence In total 13 iteration steps are needed. Although Heron's method converges quadratically close to the solution, less than one bit precision per iteration is gained at the beginning. This means that the choice of the initial estimate is critical for the performance of the algorithm. When a fast computation for the integer part of the binary logarithm or for the bit-length is available (like e.g. std::bit_width in C++20), one should better start at , which is the least power of two bigger than . In the example of the integer square root of , , , and the resulting sequence is . In this case only 4 iteration steps are needed. Digit-by-digit algorithm The traditional pen-and-paper algorithm for computing the square root is based on working from higher digit places to lower, and as each new digit pick the largest that will still yield a square . If stopping after the one's place, the result computed will be the integer square root. Using bitwise operations If working in base 2, the choice of digit is simplified to that between 0 (the "small candidate") and 1 (the "large candidate"), and digit manipulations can be expressed in terms of binary shift operations. With * being multiplication, << being left shift, and >> being logical right shift, a recursive algorithm to find the integer square root of any natural number is: def integer_sqrt(n: int) -> int: assert n >= 0, "sqrt works for only non-negative inputs" if n < 2: return n # Recursive call: small_cand = integer_sqrt(n >> 2) << 1 large_cand = small_cand + 1 if large_cand * large_cand > n: return small_cand else: return large_cand # equivalently: def integer_sqrt_iter(n: int) -> int: assert n >= 0, "sqrt works for only non-negative inputs" if n < 2: return n # Find the shift amount. See also [[find first set]], # shift = ceil(log2(n) * 0.5) * 2 = ceil(ffs(n) * 0.5) * 2 shift =