Python- Sieve of Eratosthenes- Compact Python

后端 未结 8 1170
醉话见心
醉话见心 2021-01-06 14:09

This is my code for finding primes using the Sieve of Eratosthenes.

list = [i for i in range(2, int(raw_input(\"Compute primes up to what number? \"))+1)]  
         


        
8条回答
  •  攒了一身酷
    2021-01-06 14:27

    Not exactly the the most compact solution, but the step argument in the range function in Python3 helps here -

    prime_sieve = [True] * (int(input('Primes Upto ?'))+1)
    # The first prime number
    for i in range(2, len(prime_sieve)):
        if prime_sieve[i]:
            for j in range(i+i, len(prime_sieve), i):
                prime_sieve[j] = False
            print(i, end=',')
    

提交回复
热议问题