-1

I am trying to make a function that says 'yes' if the number is a perfect square. Below is the code, please suggest the changes I need to make, considering I am a beginner.

def perfsq(x):

    if s == (math.sqrt(x)) and s * s == x:
        print('yes')

6 Answers6

1

I hope this can help:

import math

number = int(input("Enter the Number"))

root = math.sqrt(number)

if int(root + 0.5) ** 2 == number:
    print("YES")
else:
    print("NO")
  • why added 0.5 ? – inder singh Jun 06 '20 at 14:48
  • In some cases of a large number, the root may not be a perfectly whole number it might be some decimal less and the int() method takes the floor value adding 0.5 to the float number – Sorab Khanna Jun 06 '20 at 14:51
  • incase of a small number, lets say 25, why it is not giving wrong result. if int(root + 0.5) ** 2 == Number (25): ( 5 + 0.5) ** 2 != 25 – inder singh Jun 06 '20 at 16:55
  • In the case of small numbers like 25, sqrt(25) is 5 and the floor value of 5.5 will be 5. Then 5**2 will again become 25 which is equal to number which we have entered. – Sorab Khanna Jun 06 '20 at 17:16
1

example 1:

import math

def perfsq(x):
    if math.sqrt(x)%1 == 0:
        print('yes')
    else:
        print('no')

call your function:

perfsq(16)

example 2:

x = 16

print('yes') if math.sqrt(x)%1 == 0 else print('no')
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
apet
  • 958
  • 14
  • 16
0

consider this

def perfsq(x):
    s = math.sqrt(x)
    p = int(s)
    return p==s
Sai Prashanth
  • 144
  • 2
  • 11
  • 1
    Also, `if whatever: return True else: return False` is just `return whatever`. – bereal Jun 06 '20 at 13:51
  • 2
    Code only answers can almost always be improved by adding some explanation of how and why they work. In this case edit the answer to point out that you are taking the square root and then creating a copy of that value that is effectively truncated by conversion to integer in order to see if the initial result was a whole number. – Jason Aller Jun 06 '20 at 14:29
0

Your function has error since s is not defined. Your code should be as follows -

def perfsq(x):
    s = math.sqrt(x)          # You didn't define s in your code
    if s == (math.floor(s)):  # Just check if square root is an integer
        print('yes')
        return True
    else :
        print('No')
        return False
Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
0

You seem to have 2 main misunderstandings:

  1. The == is used to compare objects, not to assign. I believe you meant to assign s with the value of math.sqrt(x). This is done by s = math.sqrt(x).

  2. Even with the above change, you can't put that assignment inside the if statement. The if condition can contain expressions, not statements. Assignments are statements.


To fix that, all you need to do is first assign s separatly:

import math

def perfsq(x):
    s = math.sqrt(x) 
    if s * s == x:
        print('yes')

Now your problem is that you become dependant on float precision. sqrt returns a float, so doing operations on floats (such as s * s) is risky. As an example, using the above function, running perfsq(35) will actually print 'yes'!!!

To avoid these precision issues, it is enough to check if the square root of a number is an int to begin with. So your code can be greatly simplified to:

import math

def perfsq(x):
    if math.sqrt(x).is_integer():
        print('yes')

This is using the float.is_integer() method, based on the fact that math.sqrt always returns a float.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

Probably the easiest way is to say

def perfectSquare(x):
   return (x == math.isqrt(x) ** 2)

This algorithm is fairly fast already, since it uses Newton Method to find the integer square root. However, one can identify a lot of numbers that will never be a square. For example when you take x % 16, only remainders of 0, 1, 4, 9 need to be checked against the isqrt(x) method. If the isqrt-method is not available, one can also use the idea of the Babylonian method and combine it with the modulo idea to come up with

def perfectSquare(n):
    m = n % 16
    if m != 0 and m != 1 and m != 4 and m != 9:
      return False
    x = n
    while x * x > n:
        x = (x + n // x) // 2
    return x * x == n

For a deeper explanation, have a look on the derivation.