Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if given number is perfect square in Python
A perfect square is a number that can be expressed as the product of an integer with itself. In other words, a number is a perfect square when its square root is an integer. For example, 36 is a perfect square because ?36 = 6, and 6 is an integer.
In this tutorial, we'll explore different methods to check if a given number is a perfect square in Python.
Algorithm
To check if a number is a perfect square, we follow these steps ?
- Calculate the square root of the given number
- Take the integer part of the square root
- Square this integer and compare it with the original number
- If they match, the number is a perfect square
Method 1: Using math.sqrt()
The most straightforward approach uses Python's built-in math.sqrt() function ?
from math import sqrt
def is_perfect_square(n):
if n < 0:
return False
sq_root = int(sqrt(n))
return (sq_root * sq_root) == n
# Test with different numbers
test_numbers = [36, 25, 10, 16, 0, 1]
for num in test_numbers:
result = is_perfect_square(num)
print(f"{num} is perfect square: {result}")
36 is perfect square: True 25 is perfect square: True 10 is perfect square: False 16 is perfect square: True 0 is perfect square: True 1 is perfect square: True
Method 2: Using Integer Square Root
For better precision with large numbers, we can use the integer square root method ?
def is_perfect_square_int(n):
if n < 0:
return False
# Using integer square root to avoid floating point errors
x = n
while True:
y = (x + n // x) // 2
if y >= x:
break
x = y
return x * x == n
# Test the function
numbers = [144, 145, 169, 200]
for num in numbers:
result = is_perfect_square_int(num)
print(f"{num} is perfect square: {result}")
144 is perfect square: True 145 is perfect square: False 169 is perfect square: True 200 is perfect square: False
Method 3: Using math.isqrt() (Python 3.8+)
Python 3.8 introduced math.isqrt() which returns the integer square root ?
import math
def is_perfect_square_isqrt(n):
if n < 0:
return False
sq_root = math.isqrt(n)
return sq_root * sq_root == n
# Test with various numbers
test_cases = [49, 50, 64, 100, 121]
for num in test_cases:
result = is_perfect_square_isqrt(num)
print(f"?{num} = {math.isqrt(num)}, Perfect square: {result}")
?49 = 7, Perfect square: True ?50 = 7, Perfect square: False ?64 = 8, Perfect square: True ?100 = 10, Perfect square: True ?121 = 11, Perfect square: True
Comparison of Methods
| Method | Precision | Python Version | Best For |
|---|---|---|---|
math.sqrt() |
Good for small numbers | All versions | Simple cases |
| Integer square root | High precision | All versions | Large numbers |
math.isqrt() |
Perfect precision | 3.8+ | Modern Python |
Conclusion
Use math.isqrt() for Python 3.8+ as it provides perfect integer precision. For older versions, math.sqrt() works well for most cases, while the integer square root method handles large numbers more reliably.
