Python Find next perfect square not working in python

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Python Square
AI Thread Summary
The discussion focuses on a Python function designed to determine the next perfect square given an integer input. The initial implementation incorrectly checks if the square root of the input is an integer, leading to erroneous outputs. Participants suggest using `math.sqrt()` to compute the square root and then converting it to an integer if it is a perfect square. The logic for checking if a number is a perfect square is clarified, emphasizing that squaring the integer part of the square root should equal the original number. Several code snippets are shared, demonstrating different approaches to solving the problem, including a more readable version that separates the logic into distinct steps. The consensus is that using `isinstance()` for this check is unnecessary, as the focus should be on the value rather than the type.
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
 
Technology 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
 
  • #10
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...
 
  • #11
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!
 

Similar threads

Back
Top