RosettaCodeData/Task/Parallel-calculations/Haskell/parallel-calculations-1.hs

48 lines
975 B
Haskell
Raw Permalink Normal View History

2018-08-17 15:15:24 +01:00
import Control.Parallel.Strategies (parMap, rdeepseq)
import Control.DeepSeq (NFData)
import Data.List (maximumBy)
import Data.Function (on)
2013-10-27 22:24:23 +00:00
2014-01-17 05:32:22 +00:00
nums :: [Integer]
2018-08-17 15:15:24 +01:00
nums =
[ 112272537195293
, 112582718962171
, 112272537095293
, 115280098190773
, 115797840077099
, 1099726829285419
]
2013-10-27 22:24:23 +00:00
2018-08-17 15:15:24 +01:00
lowestFactor
:: Integral a
=> a -> a -> a
lowestFactor s n
| even n = 2
| otherwise = head y
where
y =
[ x
| x <- [s .. ceiling . sqrt $ fromIntegral n] ++ [n]
, n `rem` x == 0
, odd x ]
2013-10-27 22:24:23 +00:00
2018-08-17 15:15:24 +01:00
primeFactors
:: Integral a
=> a -> a -> [a]
2014-01-17 05:32:22 +00:00
primeFactors l n = f n l []
2018-08-17 15:15:24 +01:00
where
f n l xs =
if n > 1
then f (n `div` l) (lowestFactor (max l 3) (n `div` l)) (l : xs)
else xs
2014-01-17 05:32:22 +00:00
2018-08-17 15:15:24 +01:00
minPrimes
:: (Control.DeepSeq.NFData a, Integral a)
=> [a] -> (a, [a])
minPrimes ns =
(\(x, y) -> (x, primeFactors y x)) $
maximumBy (compare `on` snd) $ zip ns (parMap rdeepseq (lowestFactor 3) ns)
2013-10-27 22:24:23 +00:00
main :: IO ()
2018-08-17 15:15:24 +01:00
main = print $ minPrimes nums