Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this algorithm worse?
    primarykey
    data
    text
    <p>In <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow">Wikipedia</a> this is one of the given algorithms to generate prime numbers:</p> <pre><code>def eratosthenes_sieve(n): # Create a candidate list within which non-primes will be # marked as None; only candidates below sqrt(n) need be checked. candidates = [i for i in range(n + 1)] fin = int(n ** 0.5) # Loop over the candidates, marking out each multiple. for i in range(2, fin + 1): if not candidates[i]: continue candidates[i + i::i] = [None] * (n // i - 1) # Filter out non-primes and return the list. return [i for i in candidates[2:] if i] </code></pre> <p>I changed the algorithm slightly.</p> <pre><code>def eratosthenes_sieve(n): # Create a candidate list within which non-primes will be # marked as None; only candidates below sqrt(n) need be checked. candidates = [i for i in range(n + 1)] fin = int(n ** 0.5) # Loop over the candidates, marking out each multiple. candidates[4::2] = [None] * (n // 2 - 1) for i in range(3, fin + 1, 2): if not candidates[i]: continue candidates[i + i::i] = [None] * (n // i - 1) # Filter out non-primes and return the list. return [i for i in candidates[2:] if i] </code></pre> <p>I first marked off all the multiples of 2, and then I considered odd numbers only. When I timed both algorithms (tried 40.000.000) the first one was always better (albeit very slightly). I don't understand why. Can somebody please explain?</p> <p>P.S.: When I try 100.000.000, my computer freezes. Why is that? I have Core Duo E8500, 4GB RAM, Windows 7 Pro 64 Bit.</p> <p>Update 1: This is Python 3.</p> <p>Update 2: This is how I timed:</p> <pre><code>start = time.time() a = eratosthenes_sieve(40000000) end = time.time() print(end - start) </code></pre> <p><strong>UPDATE:</strong> Upon valuable comments (especially by nightcracker and Winston Ewert) I managed to code what I intended in the first place:</p> <pre><code>def eratosthenes_sieve(n): # Create a candidate list within which non-primes will be # marked as None; only c below sqrt(n) need be checked. c = [i for i in range(3, n + 1, 2)] fin = int(n ** 0.5) // 2 # Loop over the c, marking out each multiple. for i in range(fin): if not c[i]: continue c[c[i] + i::c[i]] = [None] * ((n // c[i]) - (n // (2 * c[i])) - 1) # Filter out non-primes and return the list. return [2] + [i for i in c if i] </code></pre> <p>This algorithm improves the original algorithm (mentioned at the top) by (usually) 50%. (Still, worse than the algorithm mentioned by nightcracker, naturally).</p> <p>A question to Python Masters: Is there a more Pythonic way to express this last code, in a more "functional" way?</p> <p><strong>UPDATE 2:</strong> I still couldn't decode the algorithm mentioned by nightcracker. I guess I'm too stupid.</p>
    singulars
    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.
 

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