Parallel calculations: Difference between revisions
→{{header|Haskell}}: Applied hlint, hindent. Added slightly fuller signatures and import details.
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add some comparisons using different numbers of threads to factor the same group of numbers) |
(→{{header|Haskell}}: Applied hlint, hindent. Added slightly fuller signatures and import details.) |
||
Line 837:
=={{header|Haskell}}==
<lang haskell>import Control.Parallel.Strategies (parMap, rdeepseq)
import Control.DeepSeq (NFData)
import Data.List (maximumBy)
import Data.Function (on)
nums :: [Integer]
nums =
[ 112272537195293
,
,
,
,
,
]
lowestFactor
:: Integral a
lowestFactor s n | n `rem` 2 == 0 = 2▼
=> a -> a -> a
| otherwise = head $ y▼
lowestFactor s n
where y = [x | x <- [s..ceiling . sqrt $ fromIntegral n]▼
| even n = 2
where
y =
[ x
, odd x ]
primeFactors
:: Integral a
where f n l xs = if n > 1 then f (n `div` l) (lowestFactor (max l 3) (n `div` l)) (l:xs)▼
=> a -> a -> [a]
else xs▼
primeFactors l n = f n l []
where
f n l xs =
if n > 1
minPrimes
minPrimes ns = (\(x,y) -> (x,primeFactors y x)) $▼
:: (Control.DeepSeq.NFData a, Integral a)
=> [a] -> (a, [a])
zip ns (parMap rdeepseq (lowestFactor 3) ns)▼
minPrimes ns =
main :: IO ()
▲ print $ minPrimes nums
{{out}}
<pre>(115797840077099,[212609249,544651])</pre>▼
▲(115797840077099,[212609249,544651])
==Icon and {{header|Unicon}}==
|