Perfect numbers: Difference between revisions

→‎{{header|Python}}: replaced reduce(add) with sum
(→‎{{header|Python}}: Added a slightly faster variant)
(→‎{{header|Python}}: replaced reduce(add) with sum)
Line 2,143:
Or, a little faster (by restricting the search space):
 
<lang python>from functoolsoperator import (reduceadd)
from operator import (add)
from math import (sqrt)
 
Line 2,152 ⟶ 2,151:
 
 
# perfect:: Int - > Bool
def perfect(n):
lows = filter(
Line 2,158 ⟶ 2,157:
xrange(1, 1 + int(sqrt(n)))
)
return (1 < n) and (n == reduce(
add,n == sum(
lows + concatMap(
lambda x: (
lambda y=n / x: [y] if x != y else []
)() if x != y
)(lows), 0) / 2) else []
)()
)(lows)
) / 2
)
 
 
# GENERIC -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
 
# concatMap :: (a - > [b]) - > [a] - > [b]
def concatMap(f):
return lambda xs: (
9,655

edits