Sieve of Eratosthenes - Finding Primes Python

前端 未结 17 2641
旧巷少年郎
旧巷少年郎 2020-11-22 04:40

Just to clarify, this is not a homework problem :)

I wanted to find primes for a math application I am building & came across Sieve of Eratosthenes approach.

17条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 05:07

    The fastest implementation I could come up with:

    isprime = [True]*N
    isprime[0] = isprime[1] = False
    for i in range(4, N, 2):
        isprime[i] = False
    for i in range(3, N, 2):
        if isprime[i]:
            for j in range(i*i, N, 2*i):
                isprime[j] = False
    

提交回复
热议问题