Skip to content

Instantly share code, notes, and snippets.

@dterei
Created May 1, 2010 08:58
Show Gist options
  • Save dterei/386166 to your computer and use it in GitHub Desktop.
Save dterei/386166 to your computer and use it in GitHub Desktop.
Euler Problem 14
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]
@dterei
Copy link
Author

dterei commented May 1, 2010

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

@nominolo
Copy link

nominolo commented May 1, 2010

You should probably use foldl' instead of foldl. Import it from Data.List.

@nominolo
Copy link

nominolo commented May 1, 2010

p14_fasm    3.58s
p14_viac    0.84s

(same result with foldl' -- guess the strictness analyser does a good job for this one.)

@dterei
Copy link
Author

dterei commented May 1, 2010

great thanks a lot for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment