Skip to content

Instantly share code, notes, and snippets.

@geowa4
Created February 8, 2013 01:48
Show Gist options
  • Select an option

  • Save geowa4/4735968 to your computer and use it in GitHub Desktop.

Select an option

Save geowa4/4735968 to your computer and use it in GitHub Desktop.
Project Euler Problem #14
import Data.List (foldl')
collatz :: Int -> [Int]
collatz n
| n == 1 = [1]
| even n = n : collatz (n `div` 2)
| otherwise = n : collatz (3 * n + 1)
compareCollatzTuple :: (Int, Int) -> Int -> (Int, Int)
compareCollatzTuple acc x =
if lxt > snd acc then
(x, lxt)
else
acc
where
lxt = length $ collatz x
euler14 :: Int
euler14 = fst $ foldl' compareCollatzTuple (0,0) [1..999999]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment