Last active
June 21, 2022 18:56
-
-
Save 0rphee/d6e0e5813106f1c726356c2198c0296a to your computer and use it in GitHub Desktop.
A simple program for calculating the number of prime numbers from 2 to 250000. Using `ghc -02 primes.hs` gave me around 2.9s per run.
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
isPrime :: Int -> Int | |
isPrime = helper 2 | |
where helper :: Int -> Int -> Int | |
helper count num | |
| prime = if count <= div num 2 | |
then helper (count+1) num | |
else 1 | |
| otherwise = 0 | |
where prime = (num == 2) || (mod num count /= 0) | |
sumResult :: Int | |
sumResult = sum $ map isPrime [2..250000] | |
main :: IO () | |
main = print sumResult |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment