
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Number Can Be Made Perfect Square After Adding 1 in Python
Suppose we have a number n. We have to check whether the number can be a perfect square number by adding 1 with it or not.
So, if the input is like n = 288, then the output will be True as after adding 1, it becomes 289 which is same as 17^2.
To solve this, we will follow these steps −
- res_num := n + 1
- sqrt_val := integer part of square root of(res_num)
- if sqrt_val * sqrt_val is same as res_num, then
- return True
- return False
Let us see the following implementation to get better understanding −
Example Code
from math import sqrt def solve(n): res_num = n + 1 sqrt_val = int(sqrt(res_num)) if sqrt_val * sqrt_val == res_num: return True return False n = 288 print(solve(n))
Input
288
Output
True
Advertisements