Abundant odd numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|AppleScript}}: Minor change to aliquotSum() handler.)
m (→‎{{header|Haskell}}: Tidied the Data.Numbers.Primes version – pruned out redundant import.)
Line 2,864: Line 2,864:


Or, importing Data.Numbers.Primes (and significantly faster):
Or, importing Data.Numbers.Primes (and significantly faster):
<lang haskell>import Data.Numbers.Primes
<lang haskell>import Data.List (group, sort)
import Data.List (group, sort)
import Data.Numbers.Primes
import Data.Bool (bool)


abundantTuple :: Int -> [(Int, Int)]
abundantTuple :: Int -> [(Int, Int)]
abundantTuple n
abundantTuple n =
| n < x = [(n, x)]
let x = divisorSum n
| otherwise = []
in [(n, x) | n < x]
where
x = divisorSum n


divisorSum :: Int -> Int
divisorSum :: Int -> Int
Line 2,882: Line 2,879:
foldr --
foldr --
(flip ((<*>) . fmap (*)) . scanl (*) 1)
(flip ((<*>) . fmap (*)) . scanl (*) 1)
[1] .
[1]
group . primeFactors
. group
. primeFactors


main :: IO ()
main :: IO ()
main = do
main = do
putStrLn "First 25 abundant odd numbers with their divisor sums:"
putStrLn "First 25 abundant odd numbers with their divisor sums:"
mapM_ print $ take 25 ([1,3 ..] >>= abundantTuple)
mapM_ print $ take 25 ([1, 3 ..] >>= abundantTuple)
--
--
putStrLn "\n1000th odd abundant number with its divisor sum:"
putStrLn "\n1000th odd abundant number with its divisor sum:"
print $ ([1,3 ..] >>= abundantTuple) !! 999
print $ ([1, 3 ..] >>= abundantTuple) !! 999
--
--
putStrLn "\nFirst odd abundant number over 10^9, with its divisor sum:"
putStrLn "\nFirst odd abundant number over 10^9, with its divisor sum:"
let billion = 10 ^ 9 :: Int
let billion = 10 ^ 9 :: Int
print $ head ([1 + billion,3 + billion ..] >>= abundantTuple)</lang>
print $ head ([1 + billion, 3 + billion ..] >>= abundantTuple)</lang>
{{Out}}
{{Out}}
<pre>First 25 abundant odd numbers with their divisor sums:
<pre>First 25 abundant odd numbers with their divisor sums: