我正在编码站点上解决以下问题。对于测试(隐藏测试)中的某些边缘情况,它失败了,但我不确定它们是什么。有人看到这有什么问题吗?
问题:假设A是一串所有素数按顺序压缩在一起(即235711131719...)。给定一个索引n,返回一个由 5 位数字组成的字符串,其中第一个数字n位于 A中的索引处。
例如foo(0) => 23571和foo(10) => 19232
这是我的代码:
def gen_primes():
A = {}
i = 2
while True:
if i not in A:
yield i
A[i * i] = [i]
else:
for p in A[i]:
A.setdefault(p + i, []).append(p)
del A[i]
i += 1
def answer(n):
counter = 0
prime_string = ""
for p in gen_primes():
if (counter >= n):
prime_string += str(p)
counter += len(str(p)) …Run Code Online (Sandbox Code Playgroud)