fork download
  1. import Data.List(group, insert)
  2.  
  3. primes = filterPrime [2..] where
  4. filterPrime (p:xs) = p: filterPrime [x | x <- xs, x `mod` p /= 0]
  5.  
  6. cal = product . zipWith (^) primes
  7.  
  8. count = (`div` 2) . (+1) . product . map ((+1) . (*2))
  9.  
  10. root m = head . dropWhile ((<= m) . count . map (*2)) . iterate (1:) $ []
  11.  
  12. next ns = case (group ns) of
  13. (x:xs):(y:ys):zs -> concat ((x+1:xs):(y:ys):zs): (if x==y+1 then [concat ((x:xs):(x:ys):zs)] else [])
  14. (x:xs):_ -> (x+1:xs): (if x==1 then [x:x:xs] else [])
  15.  
  16. solve m = s [] [root m] where
  17. s ms (ns:nss) = if count ns > m then s (insert (cal ns) ms) nss else s ms (nss ++ next ns)
  18. s ms _ = head ms
  19.  
  20. main = mapM_ putSolveLn [2,5,10,100,1000,10000,250000000000] where
  21. putSolveLn n = putStrLn $ show n ++ " -> " ++ (show . solve $ n)
Success #stdin #stdout 4.67s 0KB
stdin
Standard input is empty
stdout
2 -> 4
5 -> 12
10 -> 24
100 -> 1260
1000 -> 180180
10000 -> 116396280
250000000000 -> 4633408464970568987374681757947200