输入: 10
输出: 4
解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。...if i % primes[j] == 0:
break
这句代码保证了每个数最多被筛一次,将时间复杂度降到了线性。...)[1])
plt.title('Time Complexity Analysis')
plt.plot(x, y1, color='green', label='eratosthenes_sieve...')
plt.plot(x, y2, color='red', label='euler_sieve')
plt.xlabel("Parameter Range")
plt.ylabel("Time...Complexity(s)")
plt.legend() # 显示图例
总结
所以,我们在求解筛素数表的题目,只要使用 Eratosthenes 筛法就可以解决我们 80% 的问题。