# Find next perfect square not working in python

#### terabaaphoonmein

Joined Jul 19, 2020
111
Code:
def find_next_square(sq):
# Return the next square if sq is a square, -1 otherwise
sq2=(sq**1/2)
xyz=isinstance(sq2, int)
if (xyz==True):
print("Is perfect square")
nextsq=sq+1
print("Next perfect square=",nextsq**2)
else:
print("Not perfect square")
return -1

n=int(input("Enter an integer"))
find_next_square(n)
Output-:
Enter an integer25
Not perfect square

Expected output-:
Enter an integer25
Next perfect square=36

My logic is that xyz checks if $(sq)^0.5$ is integer or not. If it is integer we find next perfect square, else we return -1

Last edited:

#### ApacheKid

Joined Jan 12, 2015
1,658
Code:
def find_next_square(sq):
# Return the next square if sq is a square, -1 otherwise
sq2=(sq**1/2)
xyz=isinstance(sq2, int)
if (xyz==True):
print("Is perfect square")
nextsq=sq+1
print("Next perfect square=",nextsq**2)
else:
print("Not perfect square")
return -1

n=int(input("Enter an integer"))
find_next_square(n)
Output-:
Enter an integer25
Not perfect square

Expected output-:
Enter an integer25
Next perfect square=36

My logic is that xyz checks if $(sq)^0.5$ is integer or not. If it is integer we find next perfect square, else we return -1
Yet 25 is a perfect square...

#### terabaaphoonmein

Joined Jul 19, 2020
111
This works tbh.
Code:
import math

def find_next_square(sq):
# Return the next square if sq is a square, -1 otherwise
sq2=math.sqrt(sq)
sq2=(int(sq2) if sq2.is_integer() else sq2)  # convert answer to int if we can do it
xyz=isinstance(sq2, int)
if (xyz==True):
print("Is perfect square")
nextsq=sq2+1
print("Next perfect square=",nextsq**2)
else:
print("Not perfect square")
return -1

n=int(input("Enter an integer"))
find_next_square(n)

#### Ramussons

Joined May 3, 2013
1,417
Code:
def find_next_square(sq):
# Return the next square if sq is a square, -1 otherwise
sq2=(sq**1/2)
xyz=isinstance(sq2, int)
if (xyz==True):
print("Is perfect square")
nextsq=sq+1
print("Next perfect square=",nextsq**2)
else:
print("Not perfect square")
return -1

n=int(input("Enter an integer"))
find_next_square(n)
Output-:
Enter an integer25
Not perfect square

Expected output-:
Enter an integer25
Next perfect square=36

My logic is that xyz checks if $(sq)^0.5$ is integer or not. If it is integer we find next perfect square, else we return -1
I did this change in line 3
sq2=int((sq**1/2))
and it runs fine.

#### ZCochran98

Joined Jul 24, 2018
305
You may want to check those parentheses.
sq**1/2 is not the same as sq**(1/2). The first one is the same as $$sq^1/2$$, and the second one is $$sq^{1/2}$$
Using the int() is also a good idea, like what you had.

#### Ramussons

Joined May 3, 2013
1,417
You may want to check those parentheses.
sq**1/2 is not the same as sq**(1/2). The first one is the same as $$sq^1/2$$, and the second one is $$sq^{1/2}$$
Using the int() is also a good idea, like what you had.
As you said, 1/2 with and without brackets does make a difference.

And the int(..) that I tried earlier does not give the right answer. It wrongly declares 12 as a square because the prog is p[rogrammed to declare all Integers as Squares!!! - line 5

Last edited:

#### xox

Joined Sep 8, 2017
838
FWIW, creating a function which prints AND returns a value is probably not the best approach here. (It should really do one or the other.)

Code:
import math

while True:
value = int(input("Enter an integer: "))
root = int(math.sqrt(value))
if root**2 != value:
print(value, "is NOT perfect square")
else:
print(value, "IS perfect square and the next one is", (root + 1)**2)