2

I am working on a program right now that has the goal of analyzing twin primes in a certain way(twin primes are primes in the form of(p,p+2)). Right now I have a code that counts the twin primes remainders in % 10 form.

This is that code:

def twin_prime_counter_type10(n):
    not_prime = []
    prime = []
    A = range(n + 1)
    B = range(n + 1)
    for i in xrange(2, n+1):
        if i not in not_prime:
            prime.append(i)
            for j in xrange(i*i, n+1, i):
                not_prime.append(j)   
    for n,i in enumerate(prime):
        if not A[n] == prime[n]:
            A[i] = 1
    count1_3 = 0
    count7_9 = 0
    count9_1 = 0
    for i in B:
        if B[i] % 10 == 3 and B[i - 2] % 10 == 1:
            if A[i] * A[i-2] == 1:
                count1_3 += 1
        elif B[i] % 10 == 9 and B[i - 2] % 10 == 7:
            if A[i] * A[i-2] == 1:
                count7_9 += 1
        elif B[i] % 10 == 1 and B[i - 2] % 10 == 9:
            if A[i] * A[i-2] == 1:
                count9_1 += 1
    print count1_3
    print count7_9
    print count9_1

print sieve(10000)

This part of the code works fine but I was wondering if anyone knows of a way that when I am finding the pairs of (1)'s(the twin primes) I could also record the order they appear in the list. I don't need anyone to actually write the code that does this, I am just asking if anyone knows of a built in tool in python that I could use to preform this task.

Thanks for the help.

  • Not entirely sure what you're asking, but does `s = sieve(10000)` followed by `locations = [n for n in xrange(len(s)-1) if s[n] == 1 and s[n+1] == 1]` come anywhere close? – twalberg Aug 26 '16 at 16:50

1 Answers1

-1

assuming that sieve is the Sieve of Eratosthenes, or similar, then you can use the pairwise recipe in the itertools module to obtain only the twin primes with a generator expression

from itertools import tee #, izip # <-- for python 2

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b) #izip(a, b) # <-- for python 2

def twin_primes(n):
    return ( (p1,p2) for p1,p2 in pairwise(sieve(n)) if p1+2 == p2 )
    # or use list comprehension, but I prefer generator as they use 
    # less memory and can be infinite, as sieve can be made to be 
    # infinite as well

for example

>>> print( list(twin_primes(100)) )
[(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73)]
>>>

likewise you can use it alongside enumerate to get the index of the first prime in the pair in relation to a list of primes, for example

>>> primes=list(sieve(100))
>>> primes
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]   
>>> twins=[ (i,pair) for i,pair in enumerate( pairwise(primes) ) if pair[0]+2 == pair[1] ]
>>> twins
[(1, (3, 5)), (2, (5, 7)), (4, (11, 13)), (6, (17, 19)), (9, (29, 31)), (12, (41, 43)), (16, (59, 61)), (19, (71, 73))]
>>> primes[1]
3
>>> primes[4]
11
>>> primes[9]
29
>>> 
Community
  • 1
  • 1
Copperfield
  • 5,961
  • 2
  • 13
  • 23