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

# AOJ 1004 Pair of Primes

MAX_N = 10000

# エラトステネスの篩
prime = Array.new(MAX_N + 1, true)
prime[0] = prime[1] = false
(2..(Math.sqrt(MAX_N).to_i)).each do |k|
  next unless prime[k]

  l = 2 * k
  until l > MAX_N
    prime[l] = false
    l += k
  end
end

while line = gets
  n = line.to_i
  n_p = 0
  pair = Array.new(n + 1) {|i| n + 1 - i}

  (1..n).each do |i|
    n_p += 1 if prime[i] && prime[pair[i]]
  end

  puts n_p
end