How do I take the square root of large numbers in Python?

I am trying to check if a large number is a perfect square. Here is the relevant part of my code:

x = long(raw_input())
a = sqrt(5 * x ** 2 + 4)
b = sqrt(5 * x **2 - 4)
if long(a) == a or long(b) == b:
    print "YES"
else:
    print "NO" 

      

However, when x gets too large, I get this error:

    a = sqrt(5 * x ** 2 + 4)
OverflowError: long int too large to convert to float

      

Can someone tell me a workaround for this?

+1


source to share


1 answer


Use modulus decimal to get the square root of large numbers.



+2


source







All Articles