Skip to content

Instantly share code, notes, and snippets.

@oskimura
Created April 20, 2011 03:44
Show Gist options
  • Select an option

  • Save oskimura/930290 to your computer and use it in GitHub Desktop.

Select an option

Save oskimura/930290 to your computer and use it in GitHub Desktop.
module Main where
import Control.Applicative
import Data.List
import Data.Maybe
import Data.Function (on)
import Text.Printf
splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p [] = []
splitBy p xs = a : (splitBy p $ dropWhile p $ b)
where
(a, b) = break p xs
split :: String -> [String]
split = splitBy (==' ')
primes3 = 2:f [3] [3,5..]
where f (x:xs) ys = let (ps, qs) = span (< x^2) ys
in ps ++ f (xs ++ ps) [z | z <- qs, mod z x /= 0]
input = lines <$> readFile "input.txt"
f n m = if m==m' then "YES" else "NO"
where
[_,m'] = take 2 . dropWhile (<n) $ primes3
rr :: String -> Integer
rr = read
main :: IO ()
main =
do {ns <- (map $ map $ rr) . (map split)<$> lines<$>getContents
; mapM_ (\ [n,m] -> printf "%s\n" . f n $ m) ns
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment