Project Euler 解答

Project Euler Problem 035

Project Euler Problem 035

import Data.Char import Data.List --003より primes :: [Integer] primes = 2 : ([3,5..] `minus` join [[p*p, p*p+2*p..] | p <- primes']) where primes' = 3 : ([5,7..] `minus` join [[p*p, p*p+2*p..] | p <- primes']) join ((x:xs):t) = x : union xs (join (pairs t)) pairs ((x:xs):ys:t) = (x : union xs ys) : pairs t union (x:xs) (y:ys) = case (compare x y) of LT -> x : union xs (y:ys) EQ -> x : union xs ys GT -> y : union (x:xs) ys union xs [] = xs union [] ys = ys minus (x:xs) (y:ys) = case (compare x y) of LT -> x : minus xs (y:ys) EQ -> minus xs ys GT -> minus (x:xs) ys minus xs _ = xs factor :: Integer -> [Integer] factor n = factorimpl n primes where factorimpl n pri@(p:xs) = if p*p>n then [n] else if n`rem` p == 0 then p:factorimpl (n `quot` p) pri else factorimpl n xs divsum :: Integer -> Integer divsum n = (product. map tohi .group . factor) n - n where tohi [x] = x+1 tohi l@(x:xs) = (x^(length l+1)-1) `quot` (x-1) isprime :: Integer -> Bool isprime nn = imp nn primes where imp n (x:xs) = if x*x>n then True else if rem n x==0 then False else imp n xs rotate :: Show a => a -> Int rotate n = let (x:xs) = map digitToInt $ show n in foldl1 (\ac x -> x+ac*10) (xs++[x]) digit :: (Integral a, Num a1) => a -> a1 digit x = if x `quot` 10 /= 0 then 1 + digit (x`quot` 10) else 0 iscir :: Int -> Bool iscir x = let n = show x in if '0' `elem` n then False else case length n of 0 -> True n -> all (isprime.fromIntegral) $ take (n+1) $ iterate rotate x an :: Integer -> [Integer] an n = [x | x<-takeWhile (

since 2013