Check Perfect Square
Check whether a number is a perfect square (without using the square root function).
IntermediatePhase 1 - Level 3: Math and Number LogicExample 10 of 10
check-perfect-square.py
1# Take number2num = int(input("Enter a number: "))34# Check if perfect square5is_perfect_square = False6for i in range(1, num + 1):7 if i * i == num:8 is_perfect_square = True9 break10 if i * i > num:11 break1213if is_perfect_square:14 print("Perfect square")15else:16 print("Not a perfect square")
Output
Enter a number: 16 Perfect square Enter a number: 15 Not a perfect square
What's going on
Check if any number squared equals the input.
Key Concepts:
Loop through numbers from 1 to num
Check if i * i == num
Stop early if i * i > num (optimization)
🔗Related Content
- 📖
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- 📝
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- 💻
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum