import mathfrom typing import Generatordef slow_primes(max: int) -> Generator[int, None, None]:"""Return a list of all primes numbers up to max.>>> list(slow_primes(0))[]>>> list(slow_primes(-1))[]>>> list(slow_primes(-10))[]>>> list(slow_primes(25))[2, 3, 5, 7, 11, 13, 17, 19, 23]>>> list(slow_primes(11))[2, 3, 5, 7, 11]>>> list(slow_primes(33))[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]>>> list(slow_primes(10000))[-1]9973"""numbers: Generator = (i for i in range(1, (max + 1)))for i in (n for n in numbers if n > 1):for j in range(2, i):if (i % j) == 0:breakelse:yield idef primes(max: int) -> Generator[int, None, None]:"""Return a list of all primes numbers up to max.>>> list(primes(0))[]>>> list(primes(-1))[]>>> list(primes(-10))[]>>> list(primes(25))[2, 3, 5, 7, 11, 13, 17, 19, 23]>>> list(primes(11))[2, 3, 5, 7, 11]>>> list(primes(33))[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]>>> list(primes(10000))[-1]9973"""numbers: Generator = (i for i in range(1, (max + 1)))for i in (n for n in numbers if n > 1):# only need to check for factors up to sqrt(i)bound = int(math.sqrt(i)) + 1for j in range(2, bound):if (i % j) == 0:breakelse:yield iif __name__ == "__main__":number = int(input("Calculate primes up to:\n>> ").strip())for ret in primes(number):print(ret)# Let's benchmark them side-by-side...from timeit import timeitprint(timeit("slow_primes(1_000_000)", setup="from __main__ import slow_primes"))print(timeit("primes(1_000_000)", setup="from __main__ import primes"))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。