This Python program defines a method named isSquare that determines whether a given number is a perfect square.
It then checks a list of sample numbers to identify which ones are square numbers.
import math
def isSquare(n: int) -> bool:
if n < 0:
return False
root = int(math.sqrt(n))
return root * root == n
# Sample numbers
numbers = [12, 49, 1, 81]
# Check and print results
for num in numbers:
if isSquare(num):
print(f"{num} is a perfect square.")
else:
print(f"{num} is not a perfect square.")
12 is not a perfect square.
49 is a perfect square.
1 is a perfect square.
81 is a perfect square.
- The function isSquare uses math.sqrt to find the square root of the number
- It converts the result to an integer and squares it again
- If the squared value equals the original number, it is a perfect square
- The program checks each number in the list and prints the result