Find next perfect square not working in python

  • Context: Python 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Python Square
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
10 replies · 1K views
shivajikobardan
Messages
637
Reaction score
54
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
 
Physics news on Phys.org
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 think I found the problem
Code:
sq=25
sq2=(sq**(1/2))
print(sq2)
xyz=isinstance(sq2, int)
print(xyz)

This generates output as 5.0 and false. I need a way to get integer as sq2.
 
I need a way to do this-: If I input 25, answer should be 5 as integer. If I input 24 answer should be 4.898989486 float. Is this possible to do in python?
 
I would not use isinstance here. That is for checking the object type. Here is a quick way to check if a number if a perfect square. Can you add in the rest?

Code:
def is_perfect_square(i):
    """
    Check if a number is a perfect square.
    """
    return int(i ** 0.5) ** 2 == iprint(is_perfect_square(16))
# True
print(is_perfect_square(14))
# False
 
I tried my best. Here is what I have got. It works. But codewars isn't accepting it. IDK why? If you find errors please tell. I will try your code as well.

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)
 
Code:
def is_perfect_square(i):
    """
    Check if a number is a perfect square.
    """
    return int(i ** 0.5) ** 2 == i

n=int(input("enter a number"))
if(is_perfect_square(n)):
    print("perfect square")
    newn=n**0.5+1
    print("Next square=",newn**2)
else:
    print("Not perfect square")

I solved the code. But can you tell me what does that
return int(i ** 0.5) ** 2 == i
Line does? Please give idea. @Jameson
 
Code:
n=int(input("enter a number"))

if(int(n**0.5)**2==n):
    print("Perfect square")
    nnext=n**0.5+1
    print("Next perfect square=",nnext**2)
else:
    print("Not perfect square")

I made it this way as well.
 
I still think using isinstance() here for this check is not appropriate. You don't want to check the type of the object, you want to check the value of it.

int(i ** 0.5) ** 2 will take the square-root of the number, n, take just the integer part, then square it back. If n=9 then this will covert to 3, the square back to 9. If n=10 though it will take the square-root and get ~3.162, then take just the integer part so convert to 3, then square it to end up with 9. Since $9 \ne 10$ then n isn't a perfect square. That's the logic. We can break this up to be a little more readable like this.

Code:
import math

def is_perfect_square(i):
    """
    Check if a number is a perfect square.
    """
    int_part = int(math.sqrt(i))
    int_part_squared = int_part ** 2
    
    if int_part_squared == i:
        print(f'{i} is a perfect square!')
    else:
        print(f'{i} is not perfect square')

is_perfect_square(16)
# 16 is a perfect square!
is_perfect_square(14)
# 14 is not perfect square
 
Jameson said:
I still think using isinstance() here for this check is not appropriate. You don't want to check the type of the object, you want to check the value of it.

int(i ** 0.5) ** 2 will take the square-root of the number, n, take just the integer part, then square it back. If n=9 then this will covert to 3, the square back to 9. If n=10 though it will take the square-root and get ~3.162, then take just the integer part so convert to 3, then square it to end up with 9. Since $9 \ne 10$ then n isn't a perfect square. That's the logic. We can break this up to be a little more readable like this.

Code:
import math

def is_perfect_square(i):
    """
    Check if a number is a perfect square.
    """
    int_part = int(math.sqrt(i))
    int_part_squared = int_part ** 2
   
    if int_part_squared == i:
        print(f'{i} is a perfect square!')
    else:
        print(f'{i} is not perfect square')

is_perfect_square(16)
# 16 is a perfect square!
is_perfect_square(14)
# 14 is not perfect square
I don't think I am quite at the level of writing readable code tho...
 
shivajikobardan said:
Code:
n=int(input("enter a number"))

if(int(n**0.5)**2==n):
    print("Perfect square")
    nnext=n**0.5+1
    print("Next perfect square=",nnext**2)
else:
    print("Not perfect square")

I made it this way as well.

This looks good to me!