如何修复 Python 3 中计算的溢出错误?

How Can I Fix An Overflow Error From the Calculation In Python 3?

我创建了一个程序来计算带有米尔常数的素数,但无论如何,它会导致大量计算。例如,1.306...** 5661

它抛出溢出错误。我怎样才能解决这个问题?我试过 xrange,但它没有帮助我,因为它不再存在于 python 3 中。我不知道如何减少它。

谁能帮帮我?非常感谢!

编辑:这是代码:

theta = 1.3063778838630806904686144926026

bottom = int(input("lower bound: "))
top = int(input("high bound: "))

for i in range(0,top + 1):
    print(round(theta ** (3**i))) # causes the error

谢谢@PM 2Ring 和@Blurp!您指出 decimal 模块对我帮助很大,这正是我所需要的!原来559397567061773305900...是素数!

以下是使用整数计算密尔素数的方法。首先,您需要将 Mill 常数写成分数。我使用了 Wikipedia article 中的一个值。

num, den = 1551795687, 1187861266
print(num / den)

for i in range(1, 8):
    e = 3 ** i
    n = num ** e
    d = den ** e
    print(i, n // d)

输出

1.3063778838630806
1 2
2 11
3 1361
4 2521008887
5 16022236204009819034551083884
6 4113101149215105495247660946168530631843333312378291569324941703732418013747202413154
7 69583804376962776892757521964751417769589800913915250464742380681561387050414758147961918413247296927859465626141517084928624751186191429632740787663513270579366994745400890812584434492059975056388739246886951607326825627525396066637918379217513934013930

要执行更准确的计算,您需要使用更好的起始分数,但这会导致 nd 增长得更快。