Sequence: smallest number with exactly n divisors: Difference between revisions

→‎{{header|Haskell}}: Preferred mapMaybe to catMaybes, tidied.
(Added XPL0 example.)
(→‎{{header|Haskell}}: Preferred mapMaybe to catMaybes, tidied.)
Line 559:
=={{header|Haskell}}==
Without any subtlety or optimisation, but still fast at this modest scale:
<lang haskell>import Data.Numbers.PrimesList (primeFactorsfind, group, sort)
import Data.ListMaybe (find, group, sortmapMaybe)
import Data.MaybeNumbers.Primes (catMaybesprimeFactors)
 
------------------------- A005179 ------------------------
 
a005179 :: [Int]
a005179 =
mapMaybe
catMaybes $
( \n ->
(\n -> find ((n ==) . succ . length . properDivisors) [1 ..]) <$> [1 ..]
find
(\n -> find ((n ==) . succ . length . properDivisors) [1 ..]) <$> [1 ..]
[1 ..]
)
[1 ..]
 
--------------------------- TEST -------------------------
main :: IO ()
main = print $ take 15 a005179</lang>
 
------------------------- GENERIC ------------------------
 
properDivisors :: Int -> [Int]
properDivisors =
init .
sort . sort
. foldr
foldr --
(flip ((<*>) . fmap (*)) . scanl (*) 1)
[1] .
group . primeFactorsgroup
. primeFactors</lang>
 
main :: IO ()
main = print $ take 15 a005179</lang>
{{Out}}
<pre>[1,2,4,6,16,12,64,24,36,48,1024,60,4096,192,144]</pre>
9,655

edits