我正在尝试检查一个大数是否是完全平方数。这是我的代码的相应部分:
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"
但是,当 x 变得太大时,我会收到此错误:
a = sqrt(5 * x ** 2 + 4)
OverflowError: long int too large to convert to float
谁能告诉我解决这个问题的方法吗?
最佳答案
使用decimal module求大数的平方根。
关于python - 如何在 Python 2 中求大数的平方根?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30490439/