Check whether the number formed by concatenating two numbers is a perfect square or not in Python

PythonServer Side ProgrammingProgramming

Suppose we have two numbers x and y. We have to concatenate them and check whether the resultant number is perfect square or not.

So, if the input is like x = 2 y = 89, then the output will be True as after concatenating the number will be 289 which is 17^2.

To solve this, we will follow these steps −

  • first_num := x as string
  • second_num := y as string
  • res_num := concatenate first_num and second_num then convert to integer
  • 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(x, y):
   first_num = str(x)
   second_num = str(y)
   res_num = int(first_num + second_num)
 
   sqrt_val = int(sqrt(res_num))
  
   if sqrt_val * sqrt_val == res_num:
      return True
   return False
      
x = 2
y = 89
print(solve(x, y))

Input

2, 89

Output

True
raja
Published on 16-Jan-2021 09:29:00
Advertisements