Square root of natural numbers #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
This file defines an efficient binary implementation of the square root function that returns the
unique r
such that r * r ≤ n < (r + 1) * (r + 1)
. It takes advantage of the binary
representation by replacing the multiplication by 2 appearing in
(a + b)^2 = a^2 + 2 * a * b + b^2
by a bitmask manipulation.
Reference #
See [Wikipedia, Methods of computing square roots] (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_(base_2)).
Auxiliary function for nat.sqrt
. See e.g.
https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_(base_2)