Created
December 5, 2013 21:58
-
-
Save clrnd/7814712 to your computer and use it in GitHub Desktop.
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 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