Skip to content

Instantly share code, notes, and snippets.

@0rphee
Last active June 21, 2022 18:56
Show Gist options
  • Save 0rphee/d6e0e5813106f1c726356c2198c0296a to your computer and use it in GitHub Desktop.
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.
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