Search code examples
pythonperfect-square

Finding next perfect square


this code was supposed to find if given input is perfect square and to return next perfect square

eg n=121-->144

but I don't know why it is not working

To find next perfect square

from math import sqrt,pow

def perfect_sqr( n ):
    if n%n**0.5==0:
return pow((sqrt(n)+1),2)
perfect_sqr(121)

Solution

  • Indentation seems incorrect. Maybe just how you formatted it here? Also, taking pmaniyan's suggestion to add a print statement

    from math import sqrt,pow
    
    def perfect_sqr( n ):
        if n%n**0.5==0:
            return pow((sqrt(n)+1),2)
    print(perfect_sqr(144))
    

    Produces the desired behavior for me. Function returns None if n is not a perfect square.

    For Python 2.7, use print perfect_sqr(144)