Sieve of Eratosthenes - Finding Primes Python

前端 未结 17 2316
旧巷少年郎
旧巷少年郎 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:00

    i think this is shortest code for finding primes with eratosthenes method

    def prime(r):
        n = range(2,r)
        while len(n)>0:
            yield n[0]
            n = [x for x in n if x not in range(n[0],r,n[0])]
    
    
    print(list(prime(r)))
    

提交回复
热议问题