Skip to content
Related Articles

Related Articles

Improve Article
Python | math.isqrt() method
  • Last Updated : 04 Dec, 2020

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




# Python Program to explain
# math.isqrt() method
 
 
import math
 
n = 10
 
# Get the floor value of
# exact square root of n
sqrt = math.isqrt(n)
print(n)
 
n = 100
 
# Get the floor value of
# exact square root of n
sqrt = math.isqrt(n)
print(n)
Output: 
3
10

 

Code #2: Use of math.isqrt() method to check whether the given integer is a perfect square.

Python3




# Python Program to explain
# math.isqrt() method
 
 
import math
 
 
def isPerfect(n):
     
    # Get the floor value of
    # exact square root of n
    sqrt = math.isqrt(n)
     
    if sqrt * sqrt == n:
        print(f"{n} is perfect square")
     
    else :
        print(f"{n} is not a perfect square")
 
 
 
# Driver's code
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




# Python Program to explain
# math.isqrt() method
 
 
import math
 
n = 11
 
 
def Next(n):
     
    # Get the ceiling of
    # exact square root of n
    ceil = 1 + math.isqrt(n)
     
    # print the next perfect square of n
    print("Next perfect square of {} is {}".
          format(n, ceil*ceil))
 
 
# Driver's code
Next(11)
Next(37)
Output: 
Next perfect square after 11 is 16
Next perfect square after 37 is 49

 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :