gives an accuracy of k decimal digits.
[note 2] To compute the (entire) decimal representation of
is already defined and — for the sake of the argument — that all variables can hold integers of unlimited magnitude.
will print the entire decimal representation of
In the program above (linear search, ascending) one can replace multiplication by addition, using the equivalence
Linear search sequentially checks every value until it hits the smallest
A speed-up is achieved by using binary search instead.
This computation takes 21 iteration steps, whereas linear search (ascending, starting from
In implementations which use number formats that cannot represent all rational numbers exactly (for example, floating point), a stopping constant less than 1 should be used to protect against round-off errors.
Thus, with this method it is unnecessary to exit the field of rational numbers in order to calculate
This has the advantage of only using integers for each intermediate value, thus making the use of floating point representations of large numbers unnecessary.
is not necessarily a fixed point of the above iterative formula.
is a perfect square, the sequence ends up in a period-two cycle between
For example, if one computes the integer square root of 2000000 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
In this case only four iteration steps are needed.
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.
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: Traditional pen-and-paper presentations of the digit-by-digit algorithm include various optimizations not present in the code above, in particular the trick of pre-subtracting the square of the previous digits which makes a general multiplication step unnecessary.
See Methods of computing square roots § Binary numeral system (base 2) for an example.
[1] The Karatsuba square root algorithm is a combination of two functions: a public function, which returns the integer square root of the input, and a recursive private function, which does the majority of the work.
The public function normalizes the actual input, passes the normalized input to the private function, denormalizes the result of the private function, and returns that.
The private function takes a normalized input, divides the input bits in half, passes the most-significant half of the input recursively to the private function, and performs some integer operations on the output of that recursive call and the least-significant half of the input to get the normalized output, which it returns.
For big-integers of "50 to 1,000,000 digits", Burnikel-Ziegler Karatsuba division and Karatsuba multiplication are recommended by the algorithm's creator.
The algorithm: Some programming languages dedicate an explicit operation to the integer square root calculation in addition to the general case or can be extended by libraries to this end.