Problem:
Prime Numbers

A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.

Write a program which reads a list of N integers and prints the number of prime numbers in the list.

Input

The first line contains an integer N, the number of elements in the list.

N numbers are given in the following lines.

Output

Print the number of prime numbers in the given list.

Constraints

1 ≤ N ≤ 10000

2 ≤ an element of the list ≤ 108

Sample Input 1

5
2
3
4
5
6

Sample Output 1

3

Sample Input 2

11
7
8
9
10
11
12
13
14
15
16
17

Sample Output 2

4

Solution in PHP (judge verdict: Time Limit Exceeded)

<?php

fscanf(STDIN, '%d', $N);
$nums = array();
for ($i = 0; $i < $N; $i++) {
    fscanf(STDIN, '%d', $n);
    $nums[] = $n;
}

//与えられた整数の最も大きな値と10000を比較し小さい方
$max = min(max($nums), 10000);
//上記の値以下の素数を取得
$primes = eratosthenes($max);
$cnt = 0;

foreach ($nums as $n) {
    //最大値以下は素数配列に値があるか確認
    if ($n <= $max) {
        in_array($n, $primes) && $cnt++;
        continue;
    }

    //2の倍数は素数ではない
    if ($n % 2 === 0) {
        continue;
    }

    //10000以下の素数で割り切れるか確認
    $bool = true;
    foreach ($primes as $prime) {
        if ($n % $prime === 0) {
            $bool = false;
            break;
        }
    }

    //割り切れたので素数ではない
    if (!$bool) {
        continue;
    }

    //10001からループ(偶数は無視)
    for ($i = $max + 1; $i < $n; $i += 2) {
        if ($n % $i === 0) {
            printf("%d %% %d === not prime number\n", $n, $i);
            $bool = false;
            break;
        }
    }

    //割り切れない場合はカウント
    $bool && $cnt++;
}

echo $cnt, PHP_EOL;

//エラトステネスのふるい
function eratosthenes($n)
{
    $arr = array(0, 0) + array_fill(2, $n - 1, 1);
    for ($i = 2, $len = sqrt($n); $i <= $len; $i++) {
        if (!$arr[$i]) {
            continue;
        }

        for ($j = $i, $len2 = $n / $i; $j <= $len2; $j++) {
            $arr[$i * $j] = 0;
        }
    }

    return array_keys(array_filter($arr, function ($val) {
        return $val;
    }));
}