import Data.Char import Data.Maybe import Control.Monad import Control.Monad.ST import Data.Array.ST import Data.Array.Unboxed sieveUA :: Int -> UArray Int Bool sieveUA top = runSTUArray $ do let m = (top-1) `div` 2 r = floor . sqrt $ fromIntegral top + 1 sieve <- newArray (1,m) True -- :: ST s (STUArray s Int Bool) forM_ [1..r `div` 2] $ \i -> do isPrime <- readArray sieve i when isPrime $ do -- ((2*i+1)^2-1)`div`2 == 2*i*(i+1) forM_ [2*i*(i+1), 2*i*(i+2)+1..m] $ \j -> do writeArray sieve j False return sieve primesToUA :: Int -> [Int] primesToUA top = 2 : [i*2+1 | (i,True) <- assocs $ sieveUA top] main :: IO () main = do k <- readLn :: IO Int n <- readLn :: IO Int let ps = dropWhile ( [(Int, Int)] -> Int func1 acc [] = head acc func1 acc hps = func1 (func4 acc (func3 [] hps)) (tail hps) func2 :: Int -> (Int, Int) func2 p = (until (<10) (sum . map digitToInt . show) p, p) func3 :: [(Int, Int)] -> [(Int, Int)] -> [Int] func3 acc [] = map snd (reverse acc) func3 acc ((h,p):hps) | isJust $ lookup h acc = map snd (reverse acc) | otherwise = func3 ((h,p):acc) hps func4 :: [Int] -> [Int] -> [Int] func4 hps1 hps2 | length hps1 > length hps2 = hps1 | length hps1 < length hps2 = hps2 | otherwise = max hps1 hps2