Created
May 1, 2010 08:58
-
-
Save dterei/386166 to your computer and use it in GitHub Desktop.
Euler 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.Word | |
collatzLen :: Int -> Word32 -> Int | |
collatzLen c 1 = c | |
collatzLen c n = collatzLen (c+1) $ if n `mod` 2 == 0 then n `div` 2 else 3*n+1 | |
pmax x n = x `max` (collatzLen 1 n, n) | |
main = print . solve $ 1000000 | |
where solve xs = foldl pmax (1,1) [2..xs-1] |
You should probably use foldl'
instead of foldl
. Import it from Data.List
.
p14_fasm 3.58s
p14_viac 0.84s
(same result with foldl'
-- guess the strictness analyser does a good job for this one.)
great thanks a lot for this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To compile:
ghc --make -O2 euler_p14.hs -fasm -o p14_fasm
ghc --make -O2 euler_p14.hs -fvia-C -o p14_viac
To run:
time ./p14_fasm
time ./p14_viac