Skip to content

Instantly share code, notes, and snippets.

@isomorphism
Created August 6, 2011 05:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save isomorphism/1129054 to your computer and use it in GitHub Desktop.
Save isomorphism/1129054 to your computer and use it in GitHub Desktop.
quick and stupid benchmarking example
import Data.List
import System.Environment
{- RE: http://stackoverflow.com/q/6964392/157360
cam@atomos:~/scraps/SO/euler$ ghc -O2 --make Euler12.hs
cam@atomos:~/scraps/SO/euler$ time ./Euler12 1000
842161320
real 0m12.892s
user 0m12.885s
sys 0m0.000s
cam@atomos:~/scraps/SO/euler$ gcc -O3 -lm -o euler12c euler12.c
cam@atomos:~/scraps/SO/euler$ time ./euler12c
842161320
real 0m12.029s
user 0m11.977s
sys 0m0.000s
-}
triangleNums :: [Int]
triangleNums = scanl1 (+) [1..]
factorCount :: Int -> Int
factorCount n = (+ seed) . (* 2) . length . filter (0 ==) . map (rem n) $ [1..intSqrt]
where intSqrt = floor . sqrt $ fromIntegral n
seed = if sqrt (fromIntegral n) == fromIntegral intSqrt
then -1
else 0
euler12 :: Int -> Int
euler12 n = head $ filter ((> n) . factorCount) triangleNums
main = do [n] <- getArgs
print . euler12 $ read n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment