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).
Usually you don't use while loops for simple things like this. You rather create a range object and get the elements from there. So you could rewrite the first loop to this for example:

```

for i in range( 3, int( sqrt( n ) ) + 1, 2 ):
    if n % i == 0:
        return False

```

And it would be a lot better if you would cache your prime numbers and only check the previous prime numbers when checking a new number. You can save a lot time by that (and easily calculate larger prime numbers this way). Here is some code I wrote before to get all prime numbers up to `n` easily:

```

def primeNumbers ( end ):
    primes = []
    primes.append( 2 )

    for i in range( 3, end, 2 ):
        isPrime = True

        for j in primes:
            if i % j == 0:
                isPrime = False
                break

        if isPrime:
            primes.append( i )

    return primes

print primeNumbers( 20 )

```