Jump to content

Proper divisors: Difference between revisions

→‎{{header|Haskell}}: Reverted an over-simplification
m (→‎Python: Functional: Adjusted a couple of names for legibility)
(→‎{{header|Haskell}}: Reverted an over-simplification)
Line 2,086:
import Data.List (group, maximumBy, sort)
import Data.Ord (comparing)
import Control.Arrow ((&&&))
 
 
 
properDivisors :: Int -> [Int]
properDivisors x = init . sort . foldr (
(<*>)init . fmap (*sort) . (1 :)$
foldr
) [1] . group . primeFactors
(\(i, e) a -> (*) <$> a <*> scanl (const . (i *)) 1 [1 .. e])
 
[1]
)((head [1]&&& .length) <$> group . (primeFactors x))
 
---------------------------TEST----------------------------
 
main :: IO ()
main = do
Line 2,109 ⟶ 2,111:
maximumBy (comparing snd) $
(,) <*> (length . properDivisors) <$> [1 .. 20000]
 
 
--------------------------DISPLAY--------------------------
 
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
fTable s xShow fxShow f xs =
Line 2,117 ⟶ 2,118:
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.