作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
假设运行此程序的计算机有无限量的内存,我感兴趣的是运行以下程序时 Python 会在哪里中断:
为了好玩,我实现了 hyperoperators在 python 中作为模块 hyperop
.我的例子之一是格雷厄姆的号码:
def GrahamsNumber():
# This may take awhile...
g = 4
for n in range(1,64+1):
g = hyperop(g+2)(3,3)
return g
hyperop
类的精简版如下所示:
def __init__(self, n):
self.n = n
self.lower = hyperop(n - 1)
def _repeat(self, a, b):
if self.n == 1:
yield a
i = 1
while True:
yield a
if i == b:
break
i += 1
def __call__(self, a, b):
return reduce(lambda x, y: self.lower(y, x), self._repeat(a, b))
本质上,这个库只是一个递归的右折叠操作,对 base case of n=1 有一个特殊的定义。 .最初 __call__
被打得很漂亮:
return reduce(lambda x, y: self.lower(y, x), [a,]*b)
然而,it turns out that you can't make a list with more elements than the size of a C long .这是一个有趣的限制,大多数 Python 程序员在他们的日常工作中可能不会遇到,它激发了以下问题。
Where, if at all, will the
hyperop
calculation fail due to a technical limitation of python (specifically 2.7.10)?
最佳答案
也许 hyperop 的原始版本很健壮并且由于某些深奥的原因而失败,但是这个确切的代码失败是因为 hyperop 构造函数调用自身并引发 RuntimeError 并显示“超过最大递归深度”(在递归调用的 sys.setrecursionlimit 之后 - 这是 1000默认情况下可能在 2.7.10 中)。
关于python - 哪些技术限制阻止了 python 中格雷厄姆数的计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35542746/
我是一名优秀的程序员,十分优秀!