Project Euler #2: Even Fibonacci numbers

  • + 0 comments

    Haskell

    import Control.Applicative
    import Control.Monad
    import System.IO
    
    all_fibs = 1 : 2 : zipWith (+) fibs (tail fibs)
    fibs =  take 100 all_fibs 
    
    reduce :: [Int] -> [Int]
    reduce xs = filter (\n -> n `mod` 2 == 0) xs
    
    cutoff :: Int -> [Int] -> [Int]
    cutoff n xs = filter (\m -> m <= n && m > 0) xs
                     
    main :: IO()
    main = do
        t_temp <- getLine
        let t = read t_temp :: Int
        forM_ [1..t] $ \a0  -> do
            n_temp <- getLine
            let n = read n_temp :: Int
            
            let cutoff_fibs = cutoff n fibs  
            let even_fibs = reduce cutoff_fibs
            
            let fibSum = sum even_fibs :: Int
            putStrLn (show fibSum)