Check whether the 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

Live Demo

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

Updated on: 16-Jan-2021

369 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements