thoga31

Semiprimes (Euler, 187)

Aug 28th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- I put this as a guest first, it was a mistake...
  2.  
  3. -- ### Project Euler, problem 187 ### --
  4. listPrimes n = takeWhile (<= n) allPrimes
  5. allPrimes = 2 : filter isPrime [3,5..]
  6.   where
  7.     isPrime n = all (/= 0) $ map (n `mod`) [2..squareRoot n]
  8.     squareRoot = truncate . sqrt . fromIntegral
  9.  
  10. factors n = f n (reverse $ listPrimes n)
  11.   where
  12.     f _ [] = []
  13.     f 1 _ = []
  14.     f m l@(x:xs) | m `mod` x == 0 = x : f (m `div` x) l
  15.                  | otherwise      = f m xs
  16.  
  17. solveit n = length . filter ((==2) . length) $ map factors [1..n]
  18.  
  19. main = do
  20.   putStrLn . show $ solveit (10^8)
Advertisement
Add Comment
Please, Sign In to add comment