Find next perfect square not working in python

In summary: 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 squareNext perfect square=36
  • #1
shivajikobardan
674
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
  • #2
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
 
  • #3
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.
 
  • #4
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?
 
  • #5
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
 
  • #6
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)
 
  • #7
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
 
  • #8
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.
 
  • #9
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!
 

Related to Find next perfect square not working in python

1. Why is my code not finding the next perfect square in Python?

This could be due to a variety of reasons. Some possible explanations could be incorrect syntax, logic errors, or not properly defining the variables. It is important to carefully review your code and troubleshoot any potential issues.

2. How can I fix my code to find the next perfect square in Python?

One way to fix the code could be to use the built-in math function "sqrt" to calculate the square root of a number. This can be used in conjunction with a "while" loop to continuously check for the next perfect square until one is found.

3. Can you provide an example of code that successfully finds the next perfect square in Python?

Sure, here is an example of code using the "sqrt" function and a "while" loop to find the next perfect square of a given number:

num = int(input("Enter a number: "))sqrt = math.sqrt(num)if sqrt.is_integer(): print("The next perfect square is:", int((sqrt+1)**2))else: while not sqrt.is_integer(): num += 1 sqrt = math.sqrt(num) print("The next perfect square is:", int(sqrt**2))

4. Is there a more efficient way to find the next perfect square in Python?

Yes, there are multiple ways to optimize the code depending on the specific needs and context. One potential improvement could be to use the "ceil" function from the math module to round up the square root to the nearest integer instead of continuously incrementing the number in a loop.

5. Can this code be adapted to find the previous perfect square in Python?

Yes, the code can be easily modified to find the previous perfect square. Instead of incrementing the number in the while loop, we can decrement it until we find a perfect square. The rest of the code would remain the same.

Similar threads

  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
Replies
5
Views
997
  • Programming and Computer Science
Replies
34
Views
3K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top