Sieve of Eratosthenes - Finding Primes Python

前端 未结 17 2325
旧巷少年郎
旧巷少年郎 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条回答
  •  旧时难觅i
    2020-11-22 05:22

    import math
    def sieve(n):
        primes = [True]*n
        primes[0] = False
        primes[1] = False
        for i in range(2,int(math.sqrt(n))+1):
                j = i*i
                while j < n:
                        primes[j] = False
                        j = j+i
        return [x for x in range(n) if primes[x] == True]
    

提交回复
热议问题