Life Goes On

まあまあだけど楽しんでる方です

35問目

http://projecteuler.net/index.php?section=problems&id=35
197のように971,719と回転させても素数となるような素数が百万未満でいくつあるか求める。
素直に求めていきます。

import Data.List

main = print $ length $ euler035 1000000

euler035 :: Int -> [Int]
euler035 n = filter isCircular $ takeWhile (< n) primes

isCircular :: Int -> Bool
isCircular n = all isPrime (rotation n)

rotation :: Int -> [Int]
rotation n = map (read . cut) $ cut $ tails $ cycle $ show n
    where cut = take (length $ show n)

primes :: [Int]
primes = 2 : filter isPrime [3..]

isPrime :: Int -> Bool
isPrime x = all ((/= 0) . mod x) $
    takeWhile (<= (floor $ sqrt $ fromIntegral x)) primes