2019-01-25 21:22:20 Python

Harigami面白そうだから秘伝のエラトステネス篩を貼ってみる([2, ub]の素数の集合を返す)

Copy Copied! Full
def get_prime_set(ub): from itertools import chain from math import sqrt if ub < 4: return ({}, {}, {2}, {2, 3})[ub] ub, ub_sqrt = ub+1, int(sqrt(ub))+1 primes = {2, 3} | set(chain(range(5, ub, 6), range(7, ub, 6))) du = primes.difference_update for n in chain(range(5, ub_sqrt, 6), range(7, ub_sqrt, 6)): if n in primes: du(range(n*3, ub, n*2)) return primes
Harigami面白そうだから秘伝のエラトステネス篩を貼ってみる([2, ub]の素数の集合を返す)
RECOMMEND