Python math.isqrt() Method
The Python math.isqrt() method is used to calculate the integer square root of a non-negative integer. Mathematically, it returns the largest integer "x" such that "x2" is less than or equal to "n".
Generally, for a non-negative integer "n", the integer square root "x", denoted as isqrt(n), satisfies the condition −
x = √⌊n⌋
Where, ⌊.⌋ denotes the floor method, which returns the largest integer less than or equal to the argument. For example, if n = 25, then isqrt(25) = √⌊25⌋ = 5, because 52 = 25.
Syntax
Following is the basic syntax of the Python math.isqrt() method −
math.isqrt(n)
Parameters
This method accepts an integer as a parameter representing a non-negative number for which you want to calculate the integer square root.
Return Value
The method returns an integer, which represents the integer square root of the given value "n".
Example 1
In the following example, we are calculating the integer square root of "25" using the math.isqrt() method −
import math
result = math.isqrt(25)
print("The result obtained is:",result)
Output
The output obtained is as follows −
The result obtained is: 5
Example 2
Here, we calculate the square root of "0" using the math.isqrt() method −
import math
result = math.isqrt(0)
print("The result obtained is:",result)
Output
Following is the output of the above code −
The result obtained is: 0
Example 3
Now, we calculate the integer square root of "999999999". Since the isqrt() method returns an integer, it truncates the decimal part from the resultant value −
import math
result = math.isqrt(999999999)
print("The result is:",result)
Output
We get the output as shown below −
The result obtained is: 31622
Example 4
In this example, we use a variable "n" to store the number "144". We then calculate the integer square root of "n" using the math.isqrt() method −
import math
n = 144
result = math.isqrt(n)
print("The result obtained is:",result)
Output
The result produced is as shown below −
The result obtained is: 12