Eratosthenes筛法代码在W3Schools编辑器运行异常求助
Eratosthenes筛法代码在W3Schools编辑器运行异常问题
我在解决Eratosthenes筛法相关问题,代码在Vim编辑器里能正常运行,但提交到W3Schools编辑器时,要么显示无输出,要么提示多输出,还会出现运行时错误。我试过打印和不打印输出,完全搞不懂为什么会出现这种差异。
我的代码如下:
n = int(input()) # Use the Sieve of Eratosthenes to find and sum all primes up to n def seive_of_eratosthenes(n): prime = [True for _ in range(n+1)] prime[0] = False prime[1] = False p=2 while p*p <= n: if prime[p]: for i in range(p*p, n+1, p): prime[i] = False p += 1 primes= [p for p in range(2, n+1) if prime[p]] return sum(primes) total = sieve_of_eratosthenes(n) print(total)
该问题是W3Schools的WEEKLY009编程练习题。
内容的提问来源于stack exchange,提问作者Ashutosh Das
相关产品推荐
相关产品推荐

