Skip to content

Instantly share code, notes, and snippets.

@hollingberry
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hollingberry/728f9624750f10f56ebb to your computer and use it in GitHub Desktop.
Save hollingberry/728f9624750f10f56ebb to your computer and use it in GitHub Desktop.
How many integers from 1 to 10000 can be written as the product of two (not necessarily distinct) primes?
import Data.List
import Data.Numbers.Primes
maxValue :: Int
maxValue = 10^5
uniqOrdered :: [Int] -> [Int]
uniqOrdered = map head . group . sort
possiblePrimes :: [Int]
possiblePrimes = takeWhile (< div maxValue 2) primes
primeProducts :: [Int]
primeProducts = uniqOrdered [p1 * p2 | p1 <- possiblePrimes, p2 <- possiblePrimes, p1 * p2 < maxValue]
main :: IO ()
main = print . length $ primeProducts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment