Skip to content

Instantly share code, notes, and snippets.

@alisey
Created May 17, 2014 14:13
Show Gist options
  • Save alisey/74cd9d4585d1c8ad0628 to your computer and use it in GitHub Desktop.
Save alisey/74cd9d4585d1c8ad0628 to your computer and use it in GitHub Desktop.
Longest Collatz Sequence (http://projecteuler.net/problem=14)
import Data.Bits
collatzLen :: Int -> Int -> Int
collatzLen len x
| x == 1 = len
| testBit x 0 = collatzLen (len + 2) (shiftR (3*x + 1) 1)
| otherwise = collatzLen (len + 1) (shiftR x 1)
main = print . snd . maximum $ [(collatzLen 0 x, x) | x <- [500000..1000000]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment