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 in C (judge verdict: Accepted)

#include <stdio.h>
#include <math.h>
int main(void) {
    int n,i,j;
    while (scanf("%d",&n)!=EOF) {
        int sieve[n+1],count=0;
        for (i=2; i<=n; i++) sieve[i]=0;
        for (i=2; i<=sqrt(n); i++) {
            if (sieve[i]==0) {
                for (j=i*2; j<=n; j+=i) sieve[j]=1;
            }
        }
        if (n%2==0) {
            for (i=2; i<=n/2; i++) {
                if (sieve[i]==0&&sieve[n+1-i]==0) count++;
            }
            printf("%d\n",count*2);
        } else {
            for (i=2; i<=n/2; i++) {
                if (sieve[i]==0&&sieve[n+1-i]==0) count++;
            }
            if (sieve[n/2+1]==0) {
                printf("%d\n",count*2+1);
            } else {
                printf("%d\n",count*2);
            }
        }
    }
    return 0;
}