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)

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TypeFamilies #-}
import           Data.Maybe
import           Data.Int
import           Data.List
import           Data.Char
  -- import qualified Data.Vector.Unboxing as VU
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.ByteString.Char8 as BS
import           Data.List (scanl')
import           Data.IORef
import           Control.Monad
import           Control.Monad.ST
import           Control.Monad.State.Strict
import           Control.Applicative
import           Debug.Trace
import           Text.Printf


readInt = BS.readInt . BS.dropWhile isSpace
getVector f = VU.unfoldr f <$> BS.getLine
getIntVector = getVector readInt
getBSVector = getVector BS.uncons

main = do
  x <- readLn :: IO Int
  let primeTable = sieve $ 10^6+3
  print $ ans x primeTable

type PrimeTable = VU.Vector Bool

primeTableToList :: PrimeTable -> [Int]
primeTableToList t = [i | i <- [0..VU.length t-1], t VU.! i]

isInPrimeTable :: PrimeTable -> Int -> Bool
isInPrimeTable t n
              | 0 <= n && n < VU.length t = t VU.! n
              | otherwise = error "out of range"

sieve :: Int -> PrimeTable
sieve n = VU.create $ do
  vec <- VUM.replicate (n+1) True
  VUM.write vec 0 False
  VUM.write vec 1 False
  forM_ [2..n] $ \i -> do
    b <- VUM.read vec i
    when b $ do
      forM_ [2*i,3*i..n] $ \j -> do
        VUM.write vec j False
  return vec

ans x primeTable = loop x
  where
    loop n = if isInPrimeTable primeTable n
             then n
             else loop (n+1)