Integer square root
In number theory, the integer square root (isqrt) of a non-negative integer is the non-negative integer which is the greatest integer less than or equal to the square root of , \operatorname{isqrt}(n) = \lfloor \sqrt n \rfloor. For example, \operatorname{isqrt}(27) = \lfloor \sqrt{27} \rfloor = \lfloor 5.19615242270663 ... \rfloor = 5.
In number theory, the integer square root (isqrt) of a non-negative integer is the non-negative integer which is the greatest integer less than or equal to the square root of , \operatorname{isqrt}(n) = \lfloor \sqrt n \rfloor. For example, \operatorname{isqrt}(27) = \lfloor \sqrt{27} \rfloor = \lfloor 5.19615242270663 ... \rfloor = 5. Choose any k and compute \lfloor \sqrt {y \times 100^k} \rfloor . For example (setting y = 2 ): \begin{align} & k = 0: \lfloor \sqrt {2 \times 100^{0}} \rfloor = \lfloor \sqrt {2} \rfloor = 1 \\ & k = 1: \lfloor \sqrt {2 \times 100^{1}} \rfloor = \lfloor \sqrt {200} \rfloor = 14 \\ & k = 2: \lfloor \sqrt {2 \times 100^{2}} \rfloor = \lfloor \sqrt {20000} \rfloor = 141 \\ & k = 3: \lfloor \sqrt {2 \times 100^{3}} \rfloor = \lfloor \sqrt {2000000} \rfloor = 1414 \\ & \vdots \\ & k = 8: \lfloor \sqrt {2 \times 100^{8}} \rfloor = \lfloor \sqrt {20000000000000000} \rfloor = 141421356 \\ & \vdots \\ \end{align} Compare the results with \sqrt {2} = 1.41421356237309504880168872420969807856967187537694 ... It appears that the multiplication of the input by 100^k gives an accuracy of decimal digits. To compute the (entire) decimal representation of \sqrt y , one can execute \operatorname{isqrt}(y) an infinite number of times, increasing y by a factor 100 at each pass. Assume that in the next program ( \operatorname{sqrtForever} ) the procedure \operatorname{isqrt}(y) is already defined and — for the sake of the argument — that all variables can hold integers of unlimited magnitude. Then \operatorname{sqrtForever}(y) will print the entire decimal representation of \sqrt y . import math # assume isqrt computation as given here def sqrtForever(y: int): """ Print sqrt(y), without halting """ result = math.isqrt(y) print(str(result) + ".", end="") # print result, followed by a decimal point while True: # repeat forever ... y *= 100 # theoretical example: overflow is ignored result = math.isqrt(y) print(str(result % 10), end="") # print last digit of result The conclusion is that algorithms which compute are computationally equivalent to algorithms which compute . Another derivation of \sqrt y from \lfloor \sqrt y \rfloor is given in section Continued fraction of √c based on isqrt below. ==Basic algorithms== The integer square root of a non-negative integer y can be defined as \lfloor \sqrt y \rfloor = \max \{ x : x^2 \leq y For example, \operatorname{isqrt}(27) = \lfloor \sqrt{27} \rfloor = 5 because 5^2 \leq 27 \text{ and } 27 . ===Algorithm using linear search=== The following Python programs are straightforward implementations. ===Linear search using addition=== In the program above (linear search, ascending) one can replace multiplication by addition, using the equivalence (L+1)^2 = L^2 + 2L + 1 = L^2 + 1 + \sum_{i=1}^L 2. def isqrt(y: int) -> int: """ Integer square root (linear search, ascending) using addition """ L = 0 a = 1 d = 3 while a ===Algorithm using binary search=== Linear search sequentially checks every value until it hits the smallest x where x^2 > y . A speed-up is achieved by using binary search instead. def isqrt(y: int) -> int: """ Integer square root (binary search) """ L = 0 # lower bound of the square root R = y + 1 # upper bound of the square root while (L != R - 1): M = (L + R) // 2 # midpoint to test if (M * M Numerical examples \operatorname{isqrt}(0) = 0 , \operatorname{isqrt}(1) = 1 . Using binary search the computation of \operatorname{isqrt}(131072) converges to 362 in 17 iteration steps via the [L,R] sequence \begin{aligned} &[0,131073] \to [0,65536] \to [0,32768] \to [0,16384] \to [0,8192] \to [0,4096] \rightarrow [0,2048] \to [0,1024] \to [0,512] \\& \to [256,512] \to [256,384] \to [320,384] \to [352,384] \to [352,368] \to [360,368] \to [360,364] \to [362,364] \to [362,363] \end{aligned} The computation of \operatorname{isqrt}(2000000) converges to 1414 in 21 steps via the [L,R] sequence \begin{aligned} & [0,2000001] \to [0,1000000] \to [0,500000] \to [0,250000] \to [0,125000] \to [0,62500] \to [0,31250] \to [0,15625] \\& \to [0,7812] \to [0,3906] \to [0,1953] \to [976,1953] \to [976,1464] \to [1220,1464] \to [1342,1464] \to [1403,1464] \\& \to [1403,1433] \to [1403,1418] \to [1410,1418] \to [1414,1418] \to [1414,1416] \to [1414,1415] \end{aligned} Linear search (ascending, starting from 0 ) needs steps. ==Algorithm using Newton's method== One way of calculating \sqrt{n} and \operatorname{isqrt}(n) is to use Heron's method, which is a special case of Newton's method, to find a solution for the equation x^2 - n = 0 , giving the iterative formula x_{k+1} = \frac{1}{2}\!\left(x_k + \frac{n}{x_k}\right), \quad k \ge 0, \quad x_0 > 0. The sequence \{x_k\} converges quadratically to \sqrt{n} as k\to\infty . ===Using only integer division=== For computing \lfloor \sqrt n \rfloor one can use the quotient of Euclidean division for both of the division operations. This has the advantage of only using integers for each intermediate value, thus making the use of floating point representations unnecessary. Let n > 0 and initial guess x_0 > 0 . Define the integer sequence: x_{k+1} = \left\lfloor \frac{x_k + \left\lfloor n / x_k \right\rfloor}{2} \right\rfloor, \quad k = 0, 1, 2, \dots ===Proof of convergence=== 1. Positivity: All terms are positive integers: x_k > 0 for all k . 2. Monotonicity: If x_k > \sqrt{n} , then \lfloor n / x_k \rfloor \le n / x_k ; so x_{k+1} = \left\lfloor \frac{x_k + \lfloor n / x_k \rfloor}{2} \right\rfloor . Hence the sequence decreases. If x_k , then \lfloor n / x_k \rfloor \ge n / x_k - 1 ; so x_{k+1} \ge \frac{x_k + n / x_k - 1}{2} > x_k - 1 . Hence the sequence increases or stays the same. 3. Boundedness: The sequence is bounded below by 1 and above by x_0 , so it is bounded. 4. Stabilization / Oscillation: A bounded monotone integer sequence either stabilizes or oscillates between two consecutive integers: x_{k+1} = x_k or x_{k+1} = x_k \pm 1 . 5. Integer "Fixed-point" Condition: At stabilization or oscillation: x_{k+1} = \lfloor (x_k + \lfloor n / x_k \rfloor)/2 \rfloor . This ensures that the sequence is either at \lfloor \sqrt n \rfloor or oscillating between the two nearest integers around \sqrt n . 6. Conclusion: The sequence eventually stabilizes at \lfloor \sqrt n \rfloor or oscillates between \lfloor \sqrt n \rfloor and \lceil \sqrt n \rceil . Remark: \lfloor \sqrt n \rfloor is a strict fixed point unless n + 1 is a perfect square. If n + 1 is a perfect square, the sequence oscillates between \lfloor \sqrt n \rfloor and \lceil \sqrt n \rceil . def isqrt(n: int, x0: int = 1) -> int: """ isqrt via Newton-Heron iteration with specified initial guess. Uses 2-cycle oscillation detection. Preconditions: n >= 0 # isqrt(0) = 0 x0 > 0, defaults to 1 # initial guess Output: isqrt(n) """ assert n >= 0 and x0 > 0, "Invalid input" # isqrt(0) = 0; isqrt(1) = 1 if n One iteration is gained by setting x0 to \lfloor n/2 \rfloor with the call isqrt(2000000, 1000000) . 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. n.bit_length() in Python), one should better start at x_0 = 2^{\lfloor (\log_2 n) / 2 \rfloor + 1}, which is the least power of two bigger than \sqrt n . In the example of the integer square root of , \lfloor \log_2 n \rfloor = 20 , x_0 = 2^{11} = 2048 , and the resulting sequence is 2048 \rightarrow 1512 \rightarrow 1417 \rightarrow 1414 \rightarrow 1414. In this case only four iteration steps are needed. This corresponds to the call isqrt(2000000, 2048) . ==Digit-by-digit algorithm== The traditional pen-and-paper algorithm for computing the square root \sqrt{n} is based on working from higher digit places to lower, and as each new digit pick the largest that will still yield a square \leq n . 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 isqrt_recursive(n: int) -> int: assert n >= 0, "n must be a non-negative integer" if n > 2) n: return small_cand else: return large_cand Equivalent non-recursive program: def isqrt_iterative(x: int) -> int: """ Guy, Martin (1985). "Fast integer square root by Mr. Woo's abacus algorithm" """ assert x >= 0, "x must be a non-negative integer" op = x; res = 0 # note: i > 1 is i // 2, and i >> 2 is i // 4... # "one" starts at the highest power of four >= 2 while one != 0: dltasqr = res + one if op >= dltasqr: op -= dltasqr res += one >= 1 one >>= 2 return res See for an example. \text{Algorithm } \text{SqrtRem}(n = a_3 b^3 + a_2 b^2 + a_1 b + a_0) \text{Input: } 0 \le a_i \text{Output: } (s,r) \text{ such that } s^2 \le n = s^2 + r \qquad (s', r') \gets \text{SqrtRem}(a_3 b + a_2) \qquad (q, u) \gets \text{DivRem}(r' b + a_1, 2 s') \qquad s \gets s' b + q \qquad r \gets u b + a_0 - q^2 \qquad \text{if } r \qquad \qquad r \gets r + 2s - 1 \qquad \qquad s \gets s - 1 \qquad \text{return } (s,r) Because only one recursive call is made per level, the total complexity remains O(n) in the number of bits. Each level performs only linear-time arithmetic on half-size numbers. The Python program below implements Zimmermann’s algorithm. Given an integer n \ge 0 , SqrtRem computes simultaneously its integer square root s = \lfloor \sqrt{n} \rfloor and the corresponding remainder r = n - s^2 . The choice of isqrt() is ad libitum. def SqrtRem(n: int, word_bits: int = 32) -> tuple[int, int]: """ Implementation based on Zimmermann's Karatsuba-style integer square root algorithm [Zimmermann, 1999]. It recursively splits the input n into "limbs" of size `word_bits` and combines partial results to compute the integer square root. Args: n (int): Non-negative integer to compute the square root of. word_bits (int, optional): Number of bits per "limb" or chunk used when recursively splitting n. Default is 32. Each limb represents a fixed-size part of n for the algorithm. Returns: tuple[int, int]: s = integer square root of n, r = remainder (n - s*s). Notes: The limb size controls recursion granularity. Larger word_bits reduces recursion depth but increases the size of subproblems; smaller word_bits increases recursion depth but works on smaller chunks. Reference: Zimmermann, P. (1999). "Karatsuba Square Root", Research report #3805, Inria. Archived: https://inria.hal.science/inria-00072854v1/file/RR-3805.pdf """ if n > shift # high half, corresponds to a3*b + a2 lo = n & ((1 > quarter) # simulate Zimmermann’s DivRem step denominator = s_high Example usage for n in [(2**32) + 5, 12345678901234567890, (1 ==In programming languages== 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. Let a_0 = \lfloor \sqrt{c} \rfloor be the integer square root of c . If c is a perfect square, the continued fraction terminates immediately: \sqrt{c} = [a_0]. Otherwise, the continued fraction is periodic: \sqrt{c} = [a_0; \overline{a_1, a_2, \dots, a_m}] , where the overline indicates the repeating part. The continued fraction can be obtained by the following recurrence, which uses only integer arithmetic: m_{0} = 0, \quad d_{0} = 1, \quad a_{0} = \lfloor \sqrt{c} \rfloor. For k \geq 0 , m_{k+1} = d_{k} a_{k} - m_{k}, \quad d_{k+1} = \frac{c - m_{k+1}^{2}}{d_{k}}, \quad a_{k+1} = \left\lfloor \frac{a_{0} + m_{k+1}}{d_{k+1}} \right\rfloor. Since there are only finitely many possible triples (m_{k}, d_{k}, a_{k}) , eventually one repeats, and from that point onward the continued fraction becomes periodic. def continued_fraction_sqrt(c: int) -> tuple[int, tuple[int, ...]]: """ Compute the continued fraction of sqrt(c) using integer arithmetic. Returns [a0, (a1, a2, ..., am)] where the second element is the periodic part. For perfect squares, the period is empty. """ a0 = isqrt(c) # Perfect square: return period empty if a0 * a0 == c: return (a0, ()) m, d, a = 0, 1, a0 period = [] seen = set() while True: m_next = d * a - m d_next = (c - m_next * m_next) // d a_next = (a0 + m_next) // d_next if (m_next, d_next, a_next) in seen: break seen.add((m_next, d_next, a_next)) period.append(a_next) m, d, a = m_next, d_next, a_next return (a0, tuple(period)) Example usage for c in list(range(0, 18)) + [114] + [4097280036]: cf = continued_fraction_sqrt(c) print(f"sqrt({c}): {cf}") Output ==Notes== ==External links==
References
No reference rows published for this article.
Cite this article
MLA
"Integer square root." encyclopegia.org, 2026, https://encyclopegia.org/wiki/integer-square-root. Accessed 24 November 2026.
Chicago
"Integer square root." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/integer-square-root.
APA
encyclopegia.org. (2026). Integer square root. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/integer-square-root
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON