Write a Python program to check if a number is a perfect square. Explanation:Python Challenges - 1: Exercise-4 with Solution
Sample Solution:-
Python Code:
def is_perfect_square(n): x = n // 2 y = set([x]) while x * x != n: x = (x + (n // x)) // 2 if x in y: return False y.add(x) return True print(is_perfect_square(8)) print(is_perfect_square(9)) print(is_perfect_square(100))Sample Output:
False True TrueFlowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to check if a given positive integer is a power of four.
Next: Write a Python program to check if an integer is the power of another
integer.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Getting sorted iterable (can sort by "compare" function):
>>> a = [1, 2, -3] >>> sorted(a) [-3, 1, 2] >>> sorted(a,key=abs) [1, 2, -3]- Exercises: Weekly Top 16 Most Popular Topics
- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- JavaScript basic - Exercises, Practice, Solution
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- JavaScript conditional statements and loops - Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation
Perfect square in python | In this post, we will develop a Python program to check whether the given number is a perfect square or not.
There are different ways to check the given number is a perfect square or not. You may say that the easiest to use sqrt() method. But it is not the only way to check the given number is a perfect square or not.
Example:-
Input: 100
Output: Yes
100 is a perfect square.
10 x 10 = 100
Input: 50
Output: No
50 is not a perfect
square.
Python Program to Check Whether a Number is a Perfect Square
# Python program to check number is perfect square or not import math #importing math-module # take inputs num = int(input('Enter number: ')) root = math.sqrt(num) # check number is perfecct square if int(root + 0.5) ** 2 == num: print(num, 'is a perfect square') else: print(num, 'is not a perfect square')Output for the different input values:-
Enter number: 4
4 is a perfect square
Enter number: 5
5 is not a perfect square
Enter number: 100
100 is a perfect square
Check if Number is Perfect Square Python Program
# Python program to check number is perfect square or not import math #importing math-module def PerfectSquare(x): #user-defined function root = math.sqrt(x) # check number is perfecct square if int(root + 0.5) ** 2 == x: print(x, 'is a perfect square') else: print(x, 'is not a perfect square') return root # take inputs num = int(input('Enter number: ')) # calling function PerfectSquare(num)Output:-
Enter number: 50
50 is not a perfect square
Python Program without using sqrt() method
Check whether the given number is a perfect square or not without using the sqrt() method.
# Python program to check perfect square without sqrt() def PerfectSquare(x) : i = 1 while(i**2 <= x): if ((x%i == 0) and (x/i == i)): return True i += 1 return False # take inputs num = int(input('Enter number: ')) # calling function and display result if (PerfectSquare(num)): print(num, 'is a perfect square') else: print(num, 'is not a perfect square')Output:-
Enter number: 25
25 is a perfect square
Time complexity of this program is O(sqrt(N)).
In the below program, we will use binary search to find a number in range 1 to n whose square is equal to n. Therefore, each iteration reduces to half [1 to n/2-1 OR n/2 to n].
# Python program to check perfect square without sqrt() def PerfectSquare(x): a = 1 b = x while (a <= b): c = (a + b) >> 1 # if c is perfect square if ((c**2) == x): return True # c is small -> go b to increase c if (c**2 < x): a = c + 1 # c is large -> to a to decrease c else: b = c - 1 return False # take inputs num = int(input('Enter number: ')) # calling function and display result if (PerfectSquare(num)): print(num, 'is a perfect square') else: print(num, 'is not a perfect square')Output:-
Enter number: 10
10 is not a perfect square
Time complexity of this program is O(log(N)) and Space complexity is O(1).
Python Program to Find Perfect Square in Range
This program will be print all perfect squares from the given range.
# Python program to print perfect square in range def PerfectSquare(a, b): for i in range(a, b + 1): if (i**(.5) == int(i**(.5))): print(i, end=" ") # take inputs start = int(input('Enter start number: ')) end = int(input('Enter end number: ')) print('All perfect square') # calling function PerfectSquare(start, end)Output for the different input values:-
Enter start number: 1
Enter end
number: 50
All perfect square
1 4 9 16 25 36 49
Enter start number: 1
Enter end number: 500
All perfect square
1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484
Also See:- Python Program to Find Square root
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!