Problem:
Pair of Primes We arrange the numbers between 1 and N (1 <= N <= 10000) in increasing
order and decreasing order like this: 1 2 3 4 5 6 7 8 9 . . . N
N . . . 9 8 7 6 5 4 3 2 1 Two numbers faced each other form a pair. Your task is to compute the number of pairs P such that both
numbers in the pairs are prime. Input Input contains several test cases. Each test case consists of an integer N in
one line. Output For each line of input, output P . Sample Input 1
4
7
51 Output for the Sample Input 0
2
2
6

Solution (Python):
import math
def sieve():
    prime = [1 for i in range(10010)]
    prime[0] = 0
    prime[1] = 0
    for i in range(2, int(math.sqrt(i))):
        if (prime[i] == 1):
            for j in range(i * i, 10010, i):
                prime[j] = 0
    return prime

if __name__ == "__main__":
    prime = sieve()
    while True:
        try:
            n = input()
            d1 = [i for i in range(1, n + 1)]
            d2 = list(d1)
            d2.reverse()
            tmp = 0
            for i in range(n):
                if (prime[d1[i]] == 1 and prime[d2[i]] == 1):
                    tmp += 1
            print tmp
        except:
            break

Solution (Python):
def prime_table_boolean(n):
    list1 = [True for _ in xrange(n + 1)]
    i = 2
    while i * i <= n:
        if list1[i]:
            j = i + i
        while j <= n:
            list1[j] = False
            j += i
        i += 1
    list1[1] = False
    del list1[0]

    
    
    return list1

table = prime_table_boolean(10001)
while True:
    try:
        n = int(raw_input())
    except EOFError:
        break
    
    l = table[:n]
    r = l[::-1]
    ans = 0
    for j,k in zip(l,r):
        if j and k:
            ans += 1
    print ans

Solution (Python):
import math
import bisect
primes=[2]
top=2
nlist=[2*x-1 for x in range(2,5000)]
while top<math.sqrt(10000):
    top=nlist[0]
    primes.append(top)
    newnlist=[]
    for i in nlist:
        if i%top!=0:
            newnlist.append(i)
    nlist=newnlist
primes=primes+nlist
while(1):
    try:
        n=int(raw_input())
    except:
        break
    if n%2==0:
        if n-1 in primes:
            print 2
        else:
            print 0
    else:
        ans=0
        center=(n+1)/2
        distc=[x-center for x in primes[:bisect.bisect(primes,n)]]
        if 0 in distc:
            ans=1
            il=bisect.bisect_left(distc,0)-1
            ir=bisect.bisect_left(distc,0)+1
        else:
            il=bisect.bisect_left(distc,0)-1
            ir=bisect.bisect_left(distc,0)
        while(il>=0 and ir<len(distc)):
            if distc[il]+distc[ir]==0:
                ans=ans+2
                il=il-1
                ir=ir+1
            elif distc[il]+distc[ir]<0:
                ir=ir+1
            else:
                il=il-1
        print ans

Solution (Python):
import sys
import math

M = 10001
size = (M-1)//2
l = [1] * size
b = int(math.sqrt(M)+1)
for i in range(3,b,2):
    index = (i-3)//2
    if l[index]:
        l[index+i::i] = [0] * len(l[index+i::i])

m = []
for i in range(size):
    if l[i]:
        m.append(i)

s = ""
p = [0,0,0,1]
for i in sys.stdin:
    n = int(i)
    if n <= 3:
        print(p[n])
    elif n % 2 == 0:
        if l[(n-3)//2]:
            print(2)
        else:
            print(0)
    else:
        ct = 0
        n2 = (n-5)//2
        for j in m:
            if j > n2:
                break
            if l[n2-j]:
                ct += 1
        print(ct)

Solution (Python):
n=10000
p=[1]*(n+1)
p[0],p[1]=0,0
for i in range(2,int(n**0.5)+1):
    if p[i]:        
        for j in range(i*i,n+1,i):
            p[j]=0

while 1:
    try:n=int(input())
    except:break
    c=0
    for i in range(2,n):
        if (p[i],p[n-i+1])==(1,1):c+=1
    print(c)

Solution (Python):
primes = [0, 0] + [1]*9999
for i in range(2, 101):
    if primes[i]:
        for j in range(i*i, 10001, i):
            primes[j] = 0

while True:
    try:
        n = int(input())
        print(sum([1 for a, b in zip(list(range(1,n+1)), list(range(1,n+1)[::-1])) if primes[a] and primes[b]]))
    except:
        break

Solution (Python):
import fileinput

ps = set()
for i in range(3, 10001, 2):
    for p in ps:
        if p > 100:
            continue
        if i % p == 0:
            break
    else:
        ps.add(i)
    i += 2
ps.add(2)

for line in fileinput.input():
    n = int(line)
    print(sum(i in ps and (n + 1 - i) in ps for i in range(2, n + 1)))

Solution (Python):
while True:
	try:
		n = eval(input())
		is_prime = [True for _ in range(n+1)]
		is_prime[1] = False
		for i in range(2,int((n+1)/2)):
			if is_prime[i]:
				for j in range(i*2,n+1,i):
					is_prime[j] = False
		is_prime_rev = list(is_prime)
		is_prime_rev.reverse()
		count = 0
		for i in range(1,n+1):
			count = count + (1 if is_prime[i] and is_prime_rev[i-1] else 0)
		print(count)
	except EOFError:
		break

Solution (Python):
primes = [0, 0] + [1] * 9999
for i in range(2, 101):
    if primes[i]:
        for j in range(i*i, 10001, i):
            primes[j] = 0

while True:
    try:
        N = int(input())
    except:
        break
    print(sum(primes[i] & primes[N-i+1] for i in range(1, N+1)))

Solution (Python):
primes = [0, 0] + [1] * 9999
for i in range(2, 101):
    if primes[i]:
        for j in range(i*i, 10001, i):
            primes[j] = 0

while True:
    try:
        N = int(input())
    except:
        break
    print(sum(primes[i] and primes[N-i+1] for i in range(1, N+1)))

Solution (Python):
import sys

def is_prime(x):
    if((x % 2 == 0 and x > 2) or x < 2):
        return 0
    elif(x <= 5):
        return 1
    a = 3
    while(a * a <= x):
        if(x % a == 0):
            return 0
        a += 2
    return 1

l = []
for i in sys.stdin:
    l.append(int(i))

for data in l:
    count = 0
    data1 = [i for i in range(1,data+1)]
    data2 = [i for i in range(data,0,-1)]
    for i in range(0,data):
        count += 1 if(is_prime(data1[i]) == 1 and is_prime(data2[i]) == 1) else 0
    print(count)

Solution (Python):
import math

def ifprime(num):
  if num == 1:
    return False
  for i in range(2,math.floor(num ** 0.5) + 1):
    if num % i:
      continue
    else:
      return False
  return True

while(True):
  try:
    num = int(input())
  except:
    break
  count = 0
  for i in range(1,num+1):
    if ifprime(i) and ifprime(num+1-i):
      count += 1
  print(count)

Solution (Python):



import sys
from sys import stdin
input = stdin.readline

MAX = 10000
SQRT = 100     

notprime = [False]*MAX
def sieve():
	notprime[1] = True
	notprime[2] = False 
	for i in range(3, SQRT, 2):
		if notprime[i] == False:
			for j in range(i*i, MAX, i): notprime[j] = True
sieve()


while True:
	try: k = int(input())
	except: break
	if (k&1) == 0: print(0 if notprime[k-1] else 2)
	elif k == 3: print(1)
	else:
		ans = 0
		s, p, q = 3, 3, k-2
		while q >= s:
			if not notprime[p] and not notprime[q]: ans += 1
			p, q = p+2, q-2
		print(ans)

Solution (Python):
import math
N=10000
prime=[1]*(N+1)
prime[0],prime[1]=0,0
for i in range(2,int(math.sqrt(N))):
    if prime[i]==1:
        j=i
        while i*j<=N:
            prime[i*j]=0
            j+=1

while True:
    try:
        n=input()
    except EOFError:
        break
    cnt=0
    for i,j in zip(prime[1:n+1],prime[1:n+1][::-1]):
        if i==j==1:
            cnt+=1
    print cnt

Solution (Python):
M = 10001; p = [1]*M;
p[0],p[1] = 0,0
for i in xrange(2,101):
    if p[i]:
        for j in xrange(i*i,M,i):
            p[j] = 0
while 1:
    try:
        n = int(raw_input()); ans = 0;
        for i in xrange(1,n+1):
            if p[i] and p[n+1-i]:
                ans += 1
        print ans
    except EOFError:
        break

Solution (Python):



M = 10001
primes = [1]*M
primes[0], primes[1] = 0,0
for i in range(2,101):
    if primes[i]:
        j = 2
        while j*i < M:
            primes[j*i] = 0
            j += 1



def both_prime(num):

    
    
    if num%2 == 0:
        if primes[num-1]:
            return 2
        else:
            return 0
    
    else:
        c = 0 
        for i in range(num/2):
            if (i+1)%2==0:
                continue
            else:
                if primes[i+1] and primes[num-i]:
                    c += 1
        c *= 2
        if primes[num/2+1]:
            c += 1
        return c

if __name__=="__main__":
    while 1:
        try:
            n = int(raw_input())
            print both_prime(n)
        except:
            break

Solution (Python):
import math
r = 10000
sqrt = int(math.sqrt(r))
p = [1 for i in range(r)]
p[0] = 0
for i in range(sqrt):
    if p[i]:
        p[2*i+1::i+1] = [0 for j in range(2*i+1,r,i+1)]

while True:
    try:
        n = int(raw_input())
        count = 0
        for i in range(n):
            if p[i]*p[n-i-1] == 1:
                count += 1
        print count
    except:
        break

Solution (Python):
import math
r = 10000
sqrt = int(math.sqrt(r))
p = [1 for i in range(r)]
p[0] = 0
for i in range(sqrt):
    if p[i]:
        p[2*i+1::i+1] = [0 for j in range(2*i+1,r,i+1)]
prime = []
for i in range(r):
    if p[i]:
        prime.append(i)

while True:
    try:
        n = int(raw_input())
        count = 0
        for i in prime:
            if i > n-1:
                break
            if p[i]*p[n-i-1] == 1:
                count += 1
        print count
    except:
        break

Solution (Python):
import math
r = 10000
sqrt = int(math.sqrt(r))
p = [1 for i in range(r)]
p[0] = 0
for i in range(sqrt):
    if p[i]:
        p[2*i+1::i+1] = [0 for j in range(2*i+1,r,i+1)]
prime = []
for i in range(r):
    if p[i]:
        prime.append(i)

while True:
    try:
        n = int(raw_input())
        count = 0
        for i in prime:
            if i > n-1:
                break
            if p[i]*p[n-i-1] == 1:
                count += 1
        print count
    except:
        break

Solution (Python):
import math
r = 9999
sqrt = int(math.sqrt(r))
p = [1 for i in range(r)]
p[0] = 0
for i in range(sqrt):
    if p[i]:
        p[2*i+1::i+1] = [0 for j in range(2*i+1,r,i+1)]
prime = []
for i in range(r):
    if p[i]:
        prime.append(i)

while True:
    try:
        n = int(raw_input())
        count = 0
        for i in prime:
            if i > n-1:
                break
            if p[i]*p[n-i-1] == 1:
                count += 1
        print count
    except:
        break

Solution (Python):
import math
r = 9999
sqrt = int(math.sqrt(r))
p = [1 for i in range(r)]
p[0] = 0
for i in range(sqrt):
    if p[i]:
        p[2*i+1::i+1] = [0 for j in range(2*i+1,r,i+1)]
prime = []
for i in range(r):
    if p[i]:
        prime.append(i)

while True:
    try:
        n = int(raw_input())
        count = 0
        for i in prime:
            if i > n-1:
                break
            if p[n-i-1] == 1:
                count += 1
        print count
    except:
        break

Solution (Python):



import math as math

def prime_checker(limit):
    n = 0
    prime_index = []
    for k in range(2, limit+1):
        factor = 0
        
        
        if k % 2 == 0 and k != 2:
            continue
        
        
        for divisor in range(2, math.floor(math.sqrt(k))+1):
            if k % divisor == 0:
                factor += 1
                
        if factor == 0:
            prime_index.append(k)
            
    return prime_index

def prime_checker2(limit):
    n = 0
    prime_index = []
    for k in range(9754, limit+1):
        factor = 0
        
        
        
        if k % 2 == 0 and k != 2:
            continue
        
        
        for divisor in range(2, math.floor(math.sqrt(k))+1):
            if k % divisor == 0:
                factor += 1
                
        if factor == 0:
            prime_index.append(k)
            
    return prime_index

all_primes = prime_checker(10000)

while True:
    try:
        limit = int(input())

        if limit == 1:
            print(0)

        else:

            li = list(range(1,limit+1,1))
            li_r = list(reversed(li))

            
            index_r = []

            match_primes = [i for i in all_primes if i <= limit]
            index = match_primes
            index_r = [limit+1-j for j in match_primes]

            
            print(len(list(set(index) & set(index_r))))
    except:
        break

Solution (Python):
def isPrime(n):
    if n < 2:
        return False
    if n == 2:
        return True
    for p in range(2, int(n**0.5)+1):
        if n % p == 0:
            return False
    return True

while True:
    try:
        n=int(input())
    except:
        break
    oup=0
    if n <=1:
        print(0)
    else:
        if isPrime(n-1)==True:
            if n-1==2:
                oup+=1
            else:
                oup+=2
        for i in range(3,n//2+2,2):
            if isPrime(i)!=True:
                continue
            j=n-i+1
            if i>j:
                continue
            elif isPrime(j)==True:
                if i==j:
                    oup+=1
                else:
                    oup+=2
        print(oup)

Solution (Python):


max = 10000
j = 3
l = [1] * (max + 1)
l[1] = 0
for i in range(4,max + 1, 2):
    l[i] = 0

for i in range(3,max + 1, 2):
    if ( l[i] == 0 ):
        continue
    j = i + i 
    while ( j <= max ):
        l[j] = 0
        j = j + i 

while True:
    try:
        n  = int(input())
    except EOFError:
        break
    cnt = 0
    for i in range(1,n+1):
        if (l[i] + l[n+1-i] == 2 ):
            cnt += 1
    print(cnt)