You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 05:12:01