Innumber theory, theinteger square root (isqrt) of anon-negative integern is the non-negative integerm which is thegreatest integer less than or equal to thesquare root ofn,
For example,
Let and be non-negative integers.
Algorithms that compute (thedecimal representation of)run forever on each input which is not aperfect square.[note 1]
Algorithms that computedo not run forever. They are nevertheless capable of computing up to any desired accuracy.
Choose any and compute.
For example (setting):
Compare the results with
It appears that the multiplication of the input by gives an accuracy ofk decimal digits.[note 2]
To compute the (entire) decimal representation of, one can execute an infinite number of times, increasing by a factor at each pass.
Assume that in the next program () the procedure is already defined and — for the sake of the argument — that all variables can hold integers of unlimited magnitude.
Then will print the entire decimal representation of.[note 3]
importmath# assume isqrt computation as given heredefsqrtForever(y:int):""" Print sqrt(y), without halting """result=math.isqrt(y)print(str(result)+".",end="")# print result, followed by a decimal pointwhileTrue:# repeat forever ...y*=100# theoretical example: overflow is ignoredresult=math.isqrt(y)print(str(result%10),end="")# print last digit of result
The conclusion is that algorithms which computeisqrt() are computationally equivalent toalgorithms which computesqrt().
Another derivation of from is given in sectionContinued fraction of √c based on isqrt below.
Theinteger square root of anon-negative integer can be defined as
For example, because.
The followingPython programs are straightforward implementations.
defisqrt(y:int)->int:""" Integer square root (linear search, ascending) """# initial underestimate, L <= isqrt(y)L=0while(L+1)*(L+1)<=y:L+=1returnL
defisqrt(y:int)->int:""" Integer square root (linear search, descending) """# initial overestimate, isqrt(y) <= RR=ywhile(R*R>y):R-=1returnR
In the program above (linear search, ascending) one can replace multiplication by addition, using the equivalence
defisqrt(y:int)->int:""" Integer square root (linear search, ascending) using addition """L=0a=1d=3whilea<=y:a=a+dd=d+2L=L+1returnL
Linear search sequentially checks every value until it hits the smallest where.
A speed-up is achieved by usingbinary search instead.
defisqrt(y:int)->int:""" Integer square root (binary search) """L=0# lower bound of the square rootR=y+1# upper bound of the square rootwhile(L!=R-1):M=(L+R)//2# midpoint to testif(M*M<=y):L=Melse:R=MreturnL
Numerical examples
One way of calculating and is to useHeron's method, which is a special case ofNewton's method, to find a solution for the equation, giving the iterative formula
Thesequenceconvergesquadratically to as.[1][note 4]
For computing one can use the quotient ofEuclidean division for both of the division operations. This has the advantage of only using integers for each intermediate value, thus making the use offloating point representations unnecessary.
Let and initial guess. Define the integer sequence:
1.Positivity: All terms are positive integers: for all.
2.Monotonicity:
3.Boundedness: The sequence is bounded below by 1 and above by, so it is bounded.
4.Stabilization / Oscillation:A bounded monotone integer sequence either stabilizes or oscillates between two consecutive integers:
5.Integer "Fixed-point" Condition: At stabilization or oscillation:
6.Conclusion: The sequence eventually stabilizes at or oscillates between and.
Remark:
defisqrt(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) """assertn>=0andx0>0,"Invalid input"# isqrt(0) = 0; isqrt(1) = 1ifn<2:returnnprev2=-1# x_{i-2}prev1=x0# x_{i-1}whileTrue:x1=(prev1+n//prev1)//2# Case 1: converged (steady value)ifx1==prev1:returnx1# Case 2: oscillation (2-cycle)ifx1==prev2andx1!=prev1:# We’re flipping between prev1 and prev2# Choose the smaller one (the true integer sqrt)returnmin(prev1,x1)# Move forwardprev2,prev1=prev1,x1
The callisqrt(2000000) converges to in 14 passes throughwhile:
One iteration is gained by settingx0 to with the callisqrt(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.[note 5] When a fast computation for the integer part of thebinary logarithm or for thebit-length is available (like e.g.n.bit_length() inPython), one should better start at which is the leastpower of two bigger than. In the example of the integer square root of2000000,,, and the resulting sequence is In this case only four iteration steps are needed. This corresponds to the callisqrt(2000000, 2048).
The traditionalpen-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 inbase 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, arecursive algorithm to find the integer square root of anynatural number is:
defisqrt_recursive(n:int)->int:assertn>=0,"n must be a non-negative integer"ifn<2:returnn# Recursive call:small_cand=isqrt_recursive(n>>2)<<1# same as 2 * isqrt(n // 4)large_cand=small_cand+1iflarge_cand*large_cand>n:returnsmall_candelse:returnlarge_cand
Equivalent non-recursive program:[2][note 6]
defisqrt_iterative(x:int)->int:""" Guy, Martin (1985). "Fast integer square root by Mr. Woo's abacus algorithm" """assertx>=0,"x must be a non-negative integer"op=x;res=0# note: i << 1 is 2 * i, i << 2 is 4 * i,# i >> 1 is i // 2, and i >> 2 is i // 4...# "one" starts at the highest power of four <= xone=1whileone<=op:one<<=2one>>=2whileone!=0:dltasqr=res+oneifop>=dltasqr:op-=dltasqrres+=one<<1res>>=1one>>=2returnres
SeeMethods of computing square roots § Binary numeral system (base 2) for an example.[2]
TheKaratsuba square root algorithm applies the samedivide-and-conquer principle as theKaratsuba multiplication algorithm to compute integer square roots. The method was formally analyzed by Paul Zimmermann (1999).[3] It recursively splits the input number into high and low halves, computes the square root of the higher half, and then determines the lower half algebraically.
Paul Zimmermann (1999) gives the following algorithm.[3]
Because only one recursive call is made per level, the total complexity remains in the number of bits. Each level performs only linear-time arithmetic on half-size numbers.
| Property | Karatsuba multiplication | Karatsuba-style square root |
|---|---|---|
| Recursive calls per level | 3 | 1 |
| Recurrence | ||
| Asymptotic complexity | ||
| Key operation | Three partial multiplications and recombination | One recursive square root and algebraic correction |
The Karatsuba-style square root is mainly used forarbitrary-precision arithmetic on very large integers, where it combines efficiently withBurnikel–Ziegler division [de] andKaratsuba multiplication. It was first analyzed formally by Paul Zimmermann (1999).[3] Earlier practical work includes Martin Guy (1985),[2] and recursive versions appear inDonald Knuth (1998).[4] ModernGMP andMPIR libraries implement similar recursive techniques.
ThePython program below implements Zimmermann’s algorithm. Given an integer,SqrtRem computes simultaneously its integer square root and the corresponding remainder. The choice ofisqrt() isad libitum.[note 5]
defSqrtRem(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 """ifn<0:raiseValueError("n must be non-negative")ifn==0:return0,0# trivial case# Determine number of word-sized limbs (mimics “limb” splitting in Zimmermann)limblen=(n.bit_length()+word_bits-1)//word_bits# Base case: single limb — compute directlyiflimblen<=1:s=isqrt(n)# any isqrt, e.g., math.isqrt or customr=n-s*sreturns,r# --- Step 1: Split n into high and low parts ---half_limbs=limblen//2shift=half_limbs*word_bitshi=n>>shift# high half, corresponds to a3*b + a2lo=n&((1<<shift)-1)# low half, corresponds to a1*b + a0# --- Step 2: Recursive call on the high part ---s_high,r_high=SqrtRem(hi,word_bits)# approximate sqrt of high half# --- Step 3: Recombine to approximate full sqrt ---quarter=shift//2numerator=(r_high<<quarter)|(lo>>quarter)# simulate Zimmermann’s DivRem stepdenominator=s_high<<1# 2*s' termq=numerator//denominatorifdenominatorelse0# integer divisions_candidate=(s_high<<quarter)+q# recombine high and low# --- Step 4: Verification and correction ---# Ensure remainder is non-negative and s*s <= n < (s+1)*(s+1)s=s_candidater=n-s*swhiler<0:# overestimate corrections-=1r=n-s*swhile(s+1)*(s+1)<=n:# underestimate corrections+=1r=n-s*sreturns,r
Example usage
fornin[(2**32)+5,12345678901234567890,(1<<1512)-1]:s,r=SqrtRem(n)print(f"SqrtRem({n}) ={s}, remainder ={r}")
| Computation |
|---|
SqrtRem(4294967301) = 65536, remainder = 5 |
SqrtRem(12345678901234567890) = 3513641828, remainder = 5763386306 |
SqrtRem(143665816004337822710282600285310394341474369045835074863414468709543787931907367746179403311452034095731304066234112071267510472464260955153084575408147254672957261763907982395337943906645864229014250227057207826232751957053220218983971305018634078800548055251973907806245884614087189937340865371691338441989956445051526543084039211962387469415699218979531585795574920384684004258007709014706216763392717018544247025174258411677231986785008489302218244095) = 379032737378102767370356320425415662904513187772631008578870126471203845870697482014374611530431269030880793627229265919475483409207718357286202948008100864063587640630090308972232735749901964068667724412528434753635948938919935, remainder = 758065474756205534740712640850831325809026375545262017157740252942407691741394964028749223060862538061761587254458531838950966818415436714572405896016201728127175281260180617944465471499803928137335448825056869507271897877839870 |
Someprogramming 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.
| Programming language | Example use | Version introduced |
|---|---|---|
| Chapel | BigInteger.sqrt(result, n);[5]BigInteger.sqrtRem(result, remainder, n); | Unknown |
| Common Lisp | (isqrt n)[6] | Unknown |
| Crystal | Math.isqrt(n)[7] | 1.2 |
| Java | n.sqrt()[8] (BigInteger only) | 9 |
| Julia | isqrt(n)[9] | 0.3 |
| Maple | isqrt(n)[10] | Unknown |
| PARI/GP | sqrtint(n)[11] | 1.35a[12] (asisqrt) or before |
| PHP | sqrt($num)[13] | 4 |
| Python | math.isqrt(n)[14] | 3.8 |
| Racket | (integer-sqrt n)[15](integer-sqrt/remainder n) | Unknown |
| Ruby | Integer.sqrt(n)[16] | 2.5.0 |
| Rust | n.isqrt()[17]n.checked_isqrt()[18] | 1.84.0 |
| SageMath | isqrt(n)[19] | Unknown |
| Scheme | (exact-integer-sqrt n)[20] | R6RS |
| Tcl | isqrt($n)[21] | 8.5 |
| Zig | std.math.sqrt(n)[22] | Unknown |
The computation of thesimple continued fraction of can be carried out using only integer operations, with serving as the initial term. The algorithm[23] generates continued fraction expansionin canonical form.
Letbe the integer square root of.
If is a perfect square, the continued fraction terminates immediately:
Otherwise, the continued fraction is periodic:
where the overline indicates the repeating part.
The continued fraction can be obtained by the following recurrence, which uses only integer arithmetic:
Since there are only finitely many possible triples, eventually one repeats, and from that point onward the continued fraction becomes periodic.
On input, a non-negative integer, the following program computes thesimple continued fraction of. The integer square root is computed once.[note 5] Only integer arithmetic is used. The program outputs, where the second element is the periodic part.
defcontinued_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 emptyifa0*a0==c:return(a0,())m,d,a=0,1,a0period=[]seen=set()whileTrue:m_next=d*a-md_next=(c-m_next*m_next)//da_next=(a0+m_next)//d_nextif(m_next,d_next,a_next)inseen:breakseen.add((m_next,d_next,a_next))period.append(a_next)m,d,a=m_next,d_next,a_nextreturn(a0,tuple(period))
Example usage
forcinlist(range(0,18))+[114]+[4097280036]:cf=continued_fraction_sqrt(c)print(f"sqrt({c}):{cf}")
Output
| Input (c) | Output (cf) | Continued fraction |
|---|---|---|
| 0 | [0] | |
| 1 | [1] | |
| 2 | [1; (2,)] | |
| 3 | [1; (1, 2)] | |
| 4 | [2] | |
| 5 | [2; (4,)] | |
| 6 | [2; (2, 4)] | |
| 7 | [2; (1, 1, 1, 4)] | |
| 8 | [2; (1, 4)] | |
| 9 | [3] | |
| 10 | [3; (6,)] | |
| 11 | [3; (3, 6)] | |
| 12 | [3; (2, 6)] | |
| 13 | [3; (1, 1, 1, 1, 6)] | |
| 14 | [3; (1, 2, 1, 6)] | |
| 15 | [3; (1, 6)] | |
| 16 | [4] | |
| 17 | [4; (8,)] | |
| 114 | [10; (1, 2, 10, 2, 1, 20)] | [note 7] |
| 4097280036 | [64009; (1, 1999, 3, 4, 1, 499, 3, 1, 3, 3, 1, 124, ... ..., 3, 1, 3, 499, 1, 4, 3, 1999, 1, 128018)] period: 13,032 terms[note 8] | |
defisqrt(s:int)->int:"""isqrt via Newton/Heron iteration."""L,R=1,swhileL<R:R=L+((R-L)//2)L=s//RreturnR
Computations