· 8 years ago · Jan 24, 2018, 06:24 AM
1{-# OPTIONS_GHC -O2 -XNoMonomorphismRestriction #-}
2{-# LANGUAGE BangPatterns #-}
3{-(c) gorlum0 [at] gmail.com-}
4
5isqrt x = truncate (sqrt $ fromIntegral x)
6
7isPrime n
8 | n`rem`2 == 0 = False
9 | otherwise = null [x | x <- [3,5.. isqrt n + 1], n`rem`x == 0]
10
11-- n is prime here
12nextPrime n
13 | n == 2 = 3
14 | otherwise = head [x | x <- [n+2,n+4..], isPrime x]
15
16body :: [Int] -> [Bool]
17body [] = []
18body (n:m:xs) = (m == nextPrime n) : body xs
19
20fmt True = "YES"
21fmt False = "NO"
22
23main = do
24 ws <- words `fmap` getContents
25 let xs = map read ws
26 mapM_ (putStrLn . fmt) (body xs)