Skip to content

Instantly share code, notes, and snippets.

@clrnd
Created December 5, 2013 21:58
Show Gist options
  • Save clrnd/7814712 to your computer and use it in GitHub Desktop.
Save clrnd/7814712 to your computer and use it in GitHub Desktop.
import Text.Printf
import Control.Exception
import System.CPUTime
time :: IO t -> IO t
time a = do
start <- getCPUTime
v <- a
end <- getCPUTime
let diff = (fromIntegral (end - start)) / (10^12)
printf "Computation time: %0.3f sec\n" (diff :: Double)
return v
filtrarPrimos :: [Int] -> [Int]
filtrarPrimos [] = []
filtrarPrimos (x:xs) = x : filtrarPrimos [ a | a <- xs, mod a x /= 0 ]
criba :: [Int] -> [Int]
criba [] = []
criba (x:xs) = x : criba (filter noMultiploDeX xs)
where noMultiploDeX m = mod m x /= 0
testF f limit = last $ map (\x -> last $ f [2..x]) [2..limit]
main = do
putStrLn "criba:"
time $ show (testF criba 90000) `seq` return ()
putStrLn "Done."
putStrLn "japereira:"
time $ show (testF filtrarPrimos 90000) `seq` return ()
putStrLn "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment