Created
May 17, 2014 14:13
-
-
Save alisey/74cd9d4585d1c8ad0628 to your computer and use it in GitHub Desktop.
Longest Collatz Sequence (http://projecteuler.net/problem=14)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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