Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The given code is not very efficient. Alternative solution (just as inefficient):<sup>&dagger;</sup></p> <pre><code>&gt;&gt;&gt; from math import sqrt &gt;&gt;&gt; def is_prime(n): ... return all(n % d for d in range(2, int(sqrt(n)) + 1)) ... &gt;&gt;&gt; def primes_up_to(n): ... return filter(is_prime, range(2, n)) ... &gt;&gt;&gt; list(primes_up_to(20)) [2, 3, 5, 7, 11, 13, 17, 19] </code></pre> <p>This code uses <a href="http://docs.python.org/3.1/library/functions.html#all" rel="nofollow noreferrer"><code>all</code></a>, <a href="http://docs.python.org/3.1/library/functions.html#range" rel="nofollow noreferrer"><code>range</code></a>, <a href="http://docs.python.org/3.1/library/functions.html#int" rel="nofollow noreferrer"><code>int</code></a>, <a href="http://docs.python.org/3.1/library/math.html#math.sqrt" rel="nofollow noreferrer"><code>math.sqrt</code></a>, <a href="http://docs.python.org/3.1/library/functions.html#filter" rel="nofollow noreferrer"><code>filter</code></a> and <a href="http://docs.python.org/3.1/library/functions.html#list" rel="nofollow noreferrer"><code>list</code></a>. It is not completely identical to your code, as it prints primes <em>up to</em> a certain number, not exactly <em>n</em> primes. For that, you can do:</p> <pre><code>&gt;&gt;&gt; from itertools import count, islice &gt;&gt;&gt; def n_primes(n): ... return islice(filter(is_prime, count(2)), n) ... &gt;&gt;&gt; list(n_primes(10)) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] </code></pre> <p>That introduces another two functions, namely <a href="http://docs.python.org/3.1/library/itertools.html#itertools.count" rel="nofollow noreferrer"><code>itertools.count</code></a> and <a href="http://docs.python.org/3.1/library/itertools.html#itertools.islice" rel="nofollow noreferrer"><code>itertools.islice</code></a>. (That last piece of code works only in Python 3.x; in Python 2.x, use <a href="http://docs.python.org/library/itertools.html#itertools.ifilter" rel="nofollow noreferrer"><code>itertools.ifilter</code></a> instead of <code>filter</code>.)</p> <hr> <p>&nbsp;&nbsp;<sup>&dagger;</sup>: A more efficient method is to use the <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow noreferrer">Sieve of Eratosthenes</a>.</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.
    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.
    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