如何在 Python 2 中取大数的平方根? [英] How to take square root of large numbers in Python 2?
本文介绍了如何在 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"
但是,当 x 变得太大时,我会收到此错误:
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?
推荐答案
使用 十进制模块 取大数的平方根.
Use the decimal module to take the square root of large numbers.
这篇关于如何在 Python 2 中取大数的平方根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文