Quantcast
Viewing all articles
Browse latest Browse all 28

Answer by Tom Russell for Sieve of Eratosthenes - Finding Primes Python

I figured it must be possible to simply use the empty list as the terminating condition for the loop and came up with this:

limit = 100ints = list(range(2, limit))   # Will end up emptywhile len(ints) > 0:    prime = ints[0]    print prime    ints.remove(prime)    i = 2    multiple = prime * i    while multiple <= limit:        if multiple in ints:            ints.remove(multiple)        i += 1        multiple = prime * i

Viewing all articles
Browse latest Browse all 28

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>