Sieve of Eratosthenes - Finding Primes Python

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

    def eratosthenes(n):
        multiples = []
        for i in range(2, n+1):
            if i not in multiples:
                print (i)
                for j in range(i*i, n+1, i):
                    multiples.append(j)
    
    eratosthenes(100)
    

提交回复
热议问题