Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first thing to note is that what you have written is not the sieve of eratosthenes. Look how many loops a totally naive sieve of eratosthenes executes:</p> <pre><code>def sieve1(n): loops = 0 numbers = set(range(2, n)) for i in range(2, int(n ** 0.5) + 1): for j in range(i * 2, n, i): numbers.discard(j) loops += 1 return sorted(numbers), loops </code></pre> <p>Tested:</p> <pre><code>&gt;&gt;&gt; sieve1(100) ([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97], 178) </code></pre> <p>178 loops for 100 numbers (not including the sort). This can be improved with a few minor changes:</p> <pre><code>def sieve2(n): loops = 0 numbers = range(0, n) for prime in numbers: if prime &lt; 2: continue elif prime &gt; n ** 0.5: break for i in range(prime ** 2, n, prime): numbers[i] = 0 loops += 1 return [x for x in numbers if x &gt; 1], loops </code></pre> <p>Tested:</p> <pre><code>&gt;&gt;&gt; sieve2(100) ([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97], 102) </code></pre> <p>102 loops for 100 numbers (not including the filter at the end). Now look at yours:</p> <pre><code>def sieve3(n): loops = 0 numbers = range(2, n) for i in numbers: for j in numbers: if j != i and j % i == 0: numbers.remove(j) loops += 1 return numbers, loops </code></pre> <p>Tested:</p> <pre><code>&gt;&gt;&gt; sieve3(100) ([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97], 663) </code></pre> <p>It gets worse:</p> <pre><code>&gt;&gt;&gt; [sieve1(x)[1] for x in [100, 1000, 10000]] [178, 2978, 41723] &gt;&gt;&gt; [sieve2(x)[1] for x in [100, 1000, 10000]] [102, 1409, 16979] &gt;&gt;&gt; [sieve3(x)[1] for x in [100, 1000, 10000]] [663, 28986, 1523699] </code></pre> <p>At <code>n = 10000</code>, your implementation does almost 100x as much work!</p> <p>My suggestion would be to create a sensible implementation before making it "compact." Code golf can be fun, but it's nowhere near as challenging or as edifying as writing <em>efficient</em> code, whatever the length. </p> <p>That said, consider this one-liner (if you don't count the import, which you could get rid of by using <code>lambda x, y: x - y</code> in place of <code>operator.sub</code>). This implements the first algorithm with a small improvement:</p> <pre><code>&gt;&gt;&gt; from operator import sub &gt;&gt;&gt; reduce(sub, (set(range(x ** 2, 100, x)) for x in range(2, int(100 ** 0.5) + 1)), set(range(2, 100))) set([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload