I am trying to write a prime sieve generator that I convert to a list for printing and then print the primes in a given range. I'm pretty sure my number of pairs is correct but for some reason I am getting some extra values in my list of primes that aren't prime. (I caught this right away because my last value in the output was 3599 which is not prime). I'm not really sure if I have some kind of logical error so any help would be awesome
def sieve(n):
a = [True] * (n)
a[0] = a[1] = False
for (i, isPrime) in enumerate(a):
if isPrime:
yield i
for n in range(i*i, n, i):
a[n] = False
def pairs(li):
pair = 0
for i, x in enumerate(li):
if i < len(li)-1:
if li[i] + 2 == li[i+1]:
pair += 1
return pair
p_3600 = list(sieve(3600))
ans = [vals for vals in p_3600 if vals > 1600]
print ans
print "pairs:", pairs(ans)