Sieve of Eratosthenes - Finding Primes Python

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

    My implementation:

    import math
    n = 100
    marked = {}
    for i in range(2, int(math.sqrt(n))):
        if not marked.get(i):
            for x in range(i * i, n, i):
                marked[x] = True
    
    for i in range(2, n):
        if not marked.get(i):
            print i
    

提交回复
热议问题