xrisk
xrisk

Reputation: 3898

How to take square root of large numbers in Python 2?

I am trying to check if a large number is a perfect square. Here is the corresponding 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 becomes too large, I get this error:

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

Can anybody tell me a workaround for this?

Upvotes: 2

Views: 5632

Answers (1)

hazydev
hazydev

Reputation: 344

Use the decimal module to take the square root of large numbers.

Upvotes: 2

Related Questions