Problem:
Score: 300 points

Problem Statement

Find the minimum prime number greater than or equal to X.

Notes

A prime number is an integer greater than 1 that cannot be evenly divided by any positive integer except 1 and itself.

For example, 2, 3, and 5 are prime numbers, while 4 and 6 are not.

Constraints

2 \le X \le 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the minimum prime number greater than or equal to X.

Sample Input 1

20

Sample Output 1

23

The minimum prime number greater than or equal to 20 is 23.

Sample Input 2

2

Sample Output 2

2

X itself can be a prime number.

Sample Input 3

99992

Sample Output 3

100003

Solution in Haskell (GHC 7.10.3) (judge verdict: Accepted)

import Data.List

findPrime' :: ([Int], [Int]) -> ([Int],[Int])
findPrime' ([], primes) = ([], primes)
findPrime' (prime:numbers, primes) =
    findPrime' (filter (\x -> x `mod` prime /= 0) numbers, prime:primes)

findPrime :: Int -> [Int]
findPrime x = reverse . snd $ findPrime' ([2 .. x], [])

lowerBound' :: (Ord a) => a -> Int -> Int -> [a] -> Int
lowerBound' num left right array
    | right == left + 1 = right
    | midValue < num = lowerBound' num mid right array
    | otherwise = lowerBound' num left mid array
    where mid = (left + right) `div` 2
          midValue = array !! mid

lowerBound :: (Ord a) => a -> [a] -> Int
lowerBound num array
    | num <= head array = 0
    | last array < num = length array
    | otherwise = lowerBound' num 0 (length array - 1) array

lowerBoundValue :: (Ord a) => a -> [a] -> Maybe a
lowerBoundValue num array
    | i == length array = Nothing
    | otherwise = Just (array !! i)
    where i = lowerBound num array

isPrime :: Int -> Bool
isPrime 1 = False
isPrime 2 = True
isPrime num = foldl (\acc x -> acc && (num `mod` x /= 0)) True [2 .. mx]
    where mx = (ceiling . sqrt $ fromIntegral num) + 1

main = do
    [x] <- map read . words <$> getLine
    -- let ans = head . filter (>x) . findPrime $ 2 * x
    -- let Just ans = lowerBoundValue x (findPrime $ 2*x)
    let Just ans = find isPrime [x ..]
    putStrLn $ show ans