Find next perfect square not working in python

  • Context: Python 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Python Square
Click For Summary

Discussion Overview

The discussion revolves around a Python function intended to determine the next perfect square following a given integer input. Participants explore various approaches to check if the input is a perfect square and how to calculate the next perfect square accordingly. The conversation includes technical explanations, code snippets, and debugging efforts.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests that checking if the square root of the input is an integer is a way to determine if it is a perfect square.
  • Another participant identifies an issue with the output of the square root operation, noting that it returns a float (e.g., 5.0) instead of an integer.
  • A different approach is proposed using the `is_perfect_square` function, which checks if the square of the integer part of the square root equals the original number.
  • Some participants express skepticism about using `isinstance()` for this check, arguing that it is more relevant to evaluate the value rather than the type.
  • Several participants share their own implementations of the function, highlighting variations in logic and structure.
  • One participant requests clarification on a specific line of code that checks for perfect squares, indicating a desire for deeper understanding.

Areas of Agreement / Disagreement

There is no consensus on the best method to check for perfect squares or to implement the next perfect square function. Multiple competing views and approaches are presented, with participants debating the appropriateness of certain techniques.

Contextual Notes

Some participants' code snippets contain unresolved issues, such as the handling of float versus integer types, and there are varying opinions on the best practices for checking perfect squares.

Who May Find This Useful

Readers interested in Python programming, particularly those focused on mathematical functions and debugging code related to perfect squares, may find this discussion beneficial.

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

  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
5
Views
2K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K