Time complexity of Python 3.8's integer square root math.isqrt function
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
math.isqrt(n) returns the floor of the square root of a non-negative integer using integer arithmetic only. The important complexity detail is that it should be analyzed in terms of the bit length of n, not the numeric value of n itself. That is why simple claims such as O(log log n) are incomplete or misleading.
The Right Input Size Measure
If n has k bits, then the input size is k = n.bit_length(). For big integers, the cost of arithmetic on k-bit numbers is not constant.
That matters because math.isqrt does not just count iterations. Each refinement step performs large-integer operations whose cost grows with k.
So there are really two layers to the analysis:
- how many refinement steps occur
- how expensive the large-integer arithmetic is in each step
High-Level Algorithmic Picture
CPython's implementation is highly optimized C code that avoids floating-point conversion. The algorithm uses integer methods that converge quickly and are much better suited to arbitrary-precision integers than naive trial search.
At a high level, the number of refinement stages grows slowly with the bit length, roughly logarithmically in k or better depending on the view of the recursion. But that is not the whole runtime story, because each stage uses arithmetic on large integers.
Why O(log log n) Alone Is Not Enough
You may see O(log log n) quoted based on the number of Newton-style refinement steps. That ignores the cost of big-integer multiplication and division.
Under a realistic cost model, the runtime depends on the complexity of arithmetic on k-bit numbers. A more honest statement is this:
- iteration count grows very slowly
- overall runtime is dominated by big-integer arithmetic on numbers of size about
kbits
So in terms of input size, the function is very fast and far better than naive search, but its total cost is not captured by a unit-cost O(log log n) headline.
A Practical Way to Think About It
For everyday use, math.isqrt is effectively the right tool. It is exact, avoids floating-point precision issues, and scales very well to large integers.
A simple benchmark shows the trend without pretending to derive a closed-form proof:
The runtime grows with bit length, but much more gently than naive methods that would inspect candidates one by one.
Better Than Naive Alternatives
Compare math.isqrt conceptually with these approaches:
- trial incrementing until
x * x > n, which is hopelessly slow - binary search over candidate roots, which is much better but still less specialized
- '
int(math.sqrt(n)), which uses floating point and is not reliable for huge integers'
math.isqrt exists precisely because exact integer square roots deserve a dedicated integer algorithm.
Common Pitfalls
A common mistake is analyzing the function as if arithmetic on arbitrarily large integers were constant-time. That makes the asymptotic statement look simpler than it really is.
Another mistake is describing the complexity only in terms of the numeric value n instead of the input size k = log n. Algorithm analysis for big integers should be based on bit length.
A third issue is using math.sqrt and then casting to int for huge values. That may work for small numbers but is the wrong tool for exact large-integer roots.
Summary
- Analyze
math.isqrtin terms of bit length, not raw numeric magnitude - The function uses fast integer methods and avoids floating-point error
- A plain
O(log log n)statement is incomplete because big-integer arithmetic is not free - In practice,
math.isqrtscales very well and is the correct exact tool for integer square roots - For large integers, it is both safer and faster than floating-point or naive search approaches
Related reading
- Time complexity of Sieve of Eratosthenes algorithm
- Time complexity of System.arraycopy...?
- Time complexity of the Ford-Fulkerson method in a flow network with unit capacity edges
- Time Complexity of the Kruskal Algorithm?
- Timeout for python requests.get entire response
- Times-two faster than bit-shift, for Python 3.x integers?
- Time Complexity Of This Code Snippet
- Time Complexity of two for loops

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.