0
0
PythonProgramBeginner · 2 min read

Python Program to Check Perfect Square Number

You can check if a number is a perfect square in Python by using import math and then verifying if math.isqrt(n) ** 2 == n where n is the number.
📋

Examples

Input16
Output16 is a perfect square.
Input20
Output20 is not a perfect square.
Input0
Output0 is a perfect square.
🧠

How to Think About It

To check if a number is a perfect square, find the integer square root of the number and then square it back. If the result equals the original number, it means the number is a perfect square; otherwise, it is not.
📐

Algorithm

1
Get the input number.
2
Calculate the integer square root of the number.
3
Square the integer square root.
4
Compare the squared value with the original number.
5
If they are equal, the number is a perfect square; otherwise, it is not.
6
Return or print the result.
💻

Code

python
import math

def is_perfect_square(n):
    root = math.isqrt(n)
    return root * root == n

number = int(input("Enter a number: "))
if is_perfect_square(number):
    print(f"{number} is a perfect square.")
else:
    print(f"{number} is not a perfect square.")
Output
Enter a number: 16 16 is a perfect square.
🔍

Dry Run

Let's trace the number 16 through the code

1

Input number

number = 16

2

Calculate integer square root

root = math.isqrt(16) = 4

3

Square the root

root * root = 4 * 4 = 16

4

Compare with original number

16 == 16 is True

5

Return result

Function returns True, so print '16 is a perfect square.'

numberrootroot*rootis_perfect_square
16416True
💡

Why This Works

Step 1: Calculate integer square root

Using math.isqrt(n) finds the largest integer less than or equal to the square root of n without decimals.

Step 2: Square the root

Squaring the integer root gives a number that is either equal to or less than n.

Step 3: Compare squared root with original

If the squared root equals n, then n is a perfect square; otherwise, it is not.

🔄

Alternative Approaches

Using float square root and integer check
python
import math

def is_perfect_square(n):
    root = math.sqrt(n)
    return root.is_integer()

number = int(input("Enter a number: "))
if is_perfect_square(number):
    print(f"{number} is a perfect square.")
else:
    print(f"{number} is not a perfect square.")
This method uses floating point which can cause precision issues for very large numbers.
Using a loop to check squares (legacy approach)
python
def is_perfect_square(n):
    if n < 0:
        return False
    i = 0
    while i * i <= n:
        if i * i == n:
            return True
        i += 1
    return False

number = int(input("Enter a number: "))
if is_perfect_square(number):
    print(f"{number} is a perfect square.")
else:
    print(f"{number} is not a perfect square.")
This method is simple but inefficient for large numbers because it checks every number up to the square root.

Complexity: O(1) time, O(1) space

Time Complexity

Using math.isqrt() calculates the integer square root in constant time, so the check is O(1).

Space Complexity

The program uses a fixed amount of memory regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The math.isqrt() method is fastest and most reliable. The float method is fast but less precise. The loop method is slow and not recommended for large numbers.

ApproachTimeSpaceBest For
math.isqrt()O(1)O(1)Fast and accurate for all integers
float sqrt and is_integer()O(1)O(1)Simple but can fail on large numbers
Loop checking squaresO(√n)O(1)Educational or very small numbers only
💡
Use math.isqrt() for an accurate and fast perfect square check in Python 3.8+.
⚠️
Beginners often use floating point square root and compare with integer directly, which can fail due to precision errors.