Parallel calculations: Difference between revisions

→‎{{header|Haskell}}: Applied hlint, hindent. Added slightly fuller signatures and import details.
(→‎{{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
[ 112272537195293
, ,112582718962171
, ,112272537095293
, ,115280098190773
, ,115797840077099
, ,1099726829285419]
]
 
lowestFactor :: Integral a => a -> a -> a
:: 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
++ [n], n `rem` x == 0, x `rem` 2 /= 0]
| otherwise = head $ y
where
y =
[ x
where y = [x | x <- [s .. ceiling . sqrt $ fromIntegral n] ++ [n]
lowestFactor s n | , n `rem` 2x == 0 = 2
, odd x ]
 
primeFactors l n = f n l []
:: 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
where f n l xs = if n > 1 then f (n `div` l) (lowestFactor (max l 3) (n `div` l)) (l : xs)
else xs
 
minPrimes
minPrimes ns = (\(x,y) -> (x,primeFactors y x)) $
:: (Control.DeepSeq.NFData a, Integral a)
maximumBy (compare `on` snd) $
=> [a] -> (a, [a])
zip ns (parMap rdeepseq (lowestFactor 3) ns)
minPrimes ns =
minPrimes ns = (\(x, y) -> (x, primeFactors y x)) $
maximumBy (compare `on` snd) $ zip ns (parMap rdeepseq (lowestFactor 3) ns)
 
main :: IO ()
main = print $ minPrimes nums</lang>
main = do
print $ minPrimes nums
</lang>
{{out}}
<pre>(115797840077099,[212609249,544651])</pre>
<pre>
(115797840077099,[212609249,544651])
</pre>
 
==Icon and {{header|Unicon}}==
9,655

edits