Math module in Python contains a number of mathematical operations, which can be performed with ease using the module.
math.isqrt() method in Python is used to get the integer square root of the given non-negative integer value n. This method returns the floor value of the exact square root of n or equivalently the greatest integer a such that a2 <= n.
Note: This method is new in Python version 3.8.
Syntax: math.isqrt(n)
Parameter:
n: A non-negative integer
Returns: an integer value which represents the floor of exact square root of the given non-negative integer n.
Code #1: Use of math.isqrt() method
Python3
import math
n = 10
sqrt = math.isqrt(n)
print (sqrt)
n = 100
sqrt = math.isqrt(n)
print (sqrt)
|
Code #2: Use of math.isqrt() method to check whether the given integer is a perfect square.
Python3
import math
def isPerfect(n):
sqrt = math.isqrt(n)
if sqrt * sqrt = = n:
print (f "{n} is perfect square" )
else :
print (f "{n} is not a perfect square" )
isPerfect( 100 )
isPerfect( 10 )
|
Output: 100 is perfect square
10 is not a perfect square
Code #3: Use of math.isqrt() method to find the next perfect square of n.
Python3
import math
n = 11
def Next (n):
ceil = 1 + math.isqrt(n)
print ( "Next perfect square of {} is {}" .
format (n, ceil * ceil))
Next ( 11 )
Next ( 37 )
|
Output: Next perfect square after 11 is 16
Next perfect square after 37 is 49