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

import Control.Monad
import Control.Applicative
import Data.Array.IArray

main :: IO ()
main = do
  ns <- map read . lines <$> getContents :: IO [Int]
  mapM_ (print . solve) ns

solve :: Int -> Int
solve n = go (takeWhile (<= n) primes)
  where
    go [] = 0
    go (x:xs) = (if isPrime x && isPrime (n+1 - x)
                   then 1
                   else 0) + go xs

isPrime :: Int -> Bool
isPrime n = primeArray ! n 

primeArray :: Array Int Bool
primeArray = accumArray (||) False (1, 100001)
           (zip (takeWhile (<= 100001) primes)
            (repeat True))

primes = 2:f [3] [3,5..]
  where
    f (x:xs) ys = let (ps, qs) = span (< x^2) ys in
      ps ++ f (xs ++ ps) [z | z <- qs, mod z x /= 0]