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: Time Limit Exceeded)

using System.Linq;
using System.Collections.Generic;
using System;

public class hello
{
    public static void Main()
    {
        var p = new List<int>();
        GeneratePrime(p, 10000);
        string line;
        for (; (line = Console.ReadLine()) != null;)
        {
            var a = int.Parse(line.Trim());
            var a2 = Enumerable.Range(1, a).ToArray();
            var count = 0;
            for (int i = 0; i < a / 2; i++)
                if (p.Contains(a2[i]) && p.Contains(a2[a - i -1])) count += 2;
            if (a % 2 == 1 && p.Contains(a2[(a + 1) / 2 - 1])) count++;
            Console.WriteLine(count);
        }
    }
    public static void GeneratePrime(List<int> primeList, int m)
    {
        int prime;
        var sqrtMax = Math.Sqrt(m);
        var searchList = Enumerable.Range(2, m - 1).ToList();
        do
        {
            prime = searchList.First();
            primeList.Add(prime);
            searchList.RemoveAll(n => n % prime == 0);
        }
        while (prime < sqrtMax);
        primeList.AddRange(searchList);
    }
}