Question: I came across this (really) simple program a while ago. It just outputs the first x primes. I'm embarrassed to ask, is there any way to make it more "pythonic" ie condense it while making it (more) readable? Switching functions is fine; I'm only interested in readability.

Thanks

```
from math import sqrt

def isprime(n):
  if n ==2:
    return True
  if n % 2 ==0 : # evens
    return False

  max = int(sqrt(n))+1 #only need to search up to sqrt n 
  i=3
  while i <= max: # range starts with 3 and for odd i
    if n % i == 0:
      return False
    i+=2

  return True

reqprimes = int(input('how many primes: '))
primessofar = 0
currentnumber = 2
while primessofar < reqprimes:

  result = isprime(currentnumber)

  if result:
     primessofar+=1
     print currentnumber
     #print '\n'

  currentnumber += 1

```

Answer: 
The given code is not very efficient. Alternative solution (just as inefficient):†

```
>>> from math import sqrt
>>> def is_prime(n):
...     return all(n % d for d in range(2, int(sqrt(n)) + 1))
... 
>>> def primes_up_to(n):
...     return filter(is_prime, range(2, n))
... 
>>> list(primes_up_to(20))
[2, 3, 5, 7, 11, 13, 17, 19]

```

This code uses [`all`](http://docs.python.org/3.1/library/functions.html#all), [`range`](http://docs.python.org/3.1/library/functions.html#range), [`int`](http://docs.python.org/3.1/library/functions.html#int), [`math.sqrt`](http://docs.python.org/3.1/library/math.html#math.sqrt), [`filter`](http://docs.python.org/3.1/library/functions.html#filter) and [`list`](http://docs.python.org/3.1/library/functions.html#list). It is not completely identical to your code, as it prints primes *up to* a certain number, not exactly *n* primes. For that, you can do:

```
>>> from itertools import count, islice
>>> def n_primes(n):
...     return islice(filter(is_prime, count(2)), n)
... 
>>> list(n_primes(10))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

```

That introduces another two functions, namely [`itertools.count`](http://docs.python.org/3.1/library/itertools.html#itertools.count) and [`itertools.islice`](http://docs.python.org/3.1/library/itertools.html#itertools.islice). (That last piece of code works only in Python 3.x; in Python 2.x, use [`itertools.ifilter`](http://docs.python.org/library/itertools.html#itertools.ifilter) instead of `filter`.)

---

  †: A more efficient method is to use the [Sieve of Eratosthenes](http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes).
Translated from the brilliant guys at [stacktrace.it](http://stacktrace.it/2008/01/progetto-eulero-problema-3/) (Daniele Varrazzo, specifically), this version takes advantage of [a binary min-heap](http://en.wikipedia.org/wiki/Binary_heap) to solve this problem:

```
from heapq import heappush, heapreplace

def yield_primes():
    """Endless prime number generator."""

    # Yield 2, so we don't have to handle the empty heap special case
    yield 2

    # Heap of (non-prime, prime factor) tuples.
    todel = [ (4, 2) ]

    n = 3
    while True:
        if todel[0][0] != n:
            # This number is not on the head of the heap: prime!
            yield n
            heappush(todel, (n*n, n))   # add to heap

        else:
            # Not prime: add to heap
            while todel[0][0] == n:
                p = todel[0][1]
                heapreplace(todel, (n+p, p))
                # heapreplace pops the minimum value then pushes: 
                # heap size is unchanged

        n += 1

```

*This code isn't mine and I don't understand it fully ([but the explaination is here](http://stacktrace.it/2008/01/progetto-eulero-problema-3/) :) ), so I'm marking this answer as community wiki.*