Jump to content

Proper divisors: Difference between revisions

→‎{{header|Haskell}}: Corrected the reduced definition in terms of primeFactors
(→‎{{header|Haskell}}: Reverted an over-simplification)
(→‎{{header|Haskell}}: Corrected the reduced definition in terms of primeFactors)
Line 2,086:
import Data.List (group, maximumBy, sort)
import Data.Ord (comparing)
 
import Control.Arrow ((&&&))
 
properDivisors :: Int -> [Int]
properDivisors x =
(init . sort) $. foldr (
flip ((<*>) . fmap (*)) . scanl (*) 1
foldr
) [1] . group . primeFactors
(\(i, e) a -> (*) <$> a <*> scanl (const . (i *)) 1 [1 .. e])
 
[1]
((head &&& length) <$> group (primeFactors x))
 
---------------------------TEST----------------------------
main :: IO ()
Line 2,111 ⟶ 2,108:
maximumBy (comparing snd) $
(,) <*> (length . properDivisors) <$> [1 .. 20000]
 
 
--------------------------DISPLAY--------------------------
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
Line 2,118 ⟶ 2,115:
w = maximum (length . xShow <$> xs)
in unlines $
s  : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs</lang>
{{Out}}
<pre>Proper divisors of [1..10]:
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.