Product of divisors: Difference between revisions

m
Line 589:
 
------------------------- DIVISORS -----------------------
 
divisors
divisors :: Integral a => a -> [a]
=> a -> [a]
divisors n =
((<>) <*> (rest . reverse . fmap (quot n))) $
filter ((0 ==) . rem n) [1 .. root]
where
root = (floor . sqrt . fromIntegral) n
Line 602 ⟶ 601:
 
-------------- SUMS AND PRODUCTS OF DIVISORS -------------
 
main :: IO ()
main =
mapM_
putStrLn
[ "Sums of divisors of [1..100]:",
, test sum,
, "Products of divisors of [1..100]:",
, test product
]
 
test :: (Show a, Integral a) => ([a] -> a) -> String
test
:: (Show a, Integral a)
=> ([a] -> a) -> String
test f =
let xs = show . f . divisors <$> [1 .. 100]
w = maximum $ length <$> xs
in unlines $
in unlines $ unwords <$> fmap (fmap (justifyRight w ' ')) (chunksOf 5 xs)
unwords
<$> fmap
in unlines $ unwords <$> fmap (fmap (justifyRight w ' ')) (chunksOf 5 xs)
(chunksOf 5 xs)
 
justifyRight :: Int -> Char -> String -> String
9,655

edits