Skip to content

Instantly share code, notes, and snippets.

@orclev
Created February 16, 2012 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orclev/1841085 to your computer and use it in GitHub Desktop.
Save orclev/1841085 to your computer and use it in GitHub Desktop.
Euler problem 14
import Data.List
collatz :: Int -> Int
collatz 1 = 0
collatz n
| odd n = force $ (collatz $! 3*n+1) + 1
| even n = force $ (collatz $! div n 2) + 1
force x = x `seq` x
longest = foldl' check 0 [1..1000000]
where
check a b = if collatz b > a then collatz b else a
main = putStrLn . show $ longest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment