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

import java.util.*;
import java.io.*;
import static java.lang.Math.*;

class Main {

    static int[] makePrimes( final int lim ) {
	
	BitSet isPrime = new BitSet( lim + 1 );
	isPrime.set( 0, lim + 1, true );
	isPrime.set( 0, 2, false );
	final int limIndex = ( int )sqrt( lim );
	for ( int index = 2; index <= limIndex; index++ ) {
	    if ( isPrime.get( index ) ) {
	   	for ( int i = index * index; i <= lim; i += index ) {
		    isPrime.set( i, false );
		}
	    }
	}
	final int count = isPrime.cardinality();
	int[] ans = new int[count];
	int index = 0;
	for ( int i = 0; i <= lim; i++ ) {
	    if ( isPrime.get( i ) ) {
		ans[index++] = i;
	    }
	}
	return ans;
    }
    
    public static void main( final String[] args ) {

	final Scanner stdin = new Scanner( System.in );
	final int [] primes = makePrimes( 10000 );
	
	while ( stdin.hasNextInt() ) {

	    final int n = stdin.nextInt();
	    if ( n <= 2 ) {
		System.out.println( 0 );
	    } else {
		int count = 0;
		int index = 0;
		while ( index < primes.length && primes[ index ] <= n ) {
		    if ( Arrays.binarySearch( primes, n - primes[ index ] + 1 ) >= 0 ) {
			count++;
		    }
		    index++;
		}
		System.out.println( count );
	    }
	}	
    }    
}