Jump to content

Perfect numbers: Difference between revisions

m
→‎{{header|Python}}: Minor updates for Python3
m (→‎{{header|Python}}: Minor updates for Python3)
Line 2,325:
Or, a little faster (by restricting the search space):
 
<lang python>from operatorfunctools import (addreduce)
from operator import (add)
from math import (sqrt)
 
Line 2,331 ⟶ 2,332:
# main :: IO ()
def main():
print(filter(perfect, xrange(1, 10000)))
list(filter(perfect, enumFromTo(1)(10000)))
)
 
 
# perfect :: Int - > Bool
def perfect(n):
lows = list(filter(
lambda x: 0 == (n % x),
xrangeenumFromTo(1, 1 + )(int(sqrt(n)))
))
return (1 < n) and(
n == sum(
Line 2,355 ⟶ 2,358:
# GENERIC --------------------------------
 
# concatMap :: (a - > [b]) - > [a] - > [b]
def concatMap(f):
return lambdadef go(xs): (
reduce(add,a map(f, xs),= [])
for x in xs:
)
a = a + f(x)
return a
return lambda xs: go(xs)
 
 
# enumFromTo :: Int -> Int -> [Int]
def enumFromTo(m):
return lambda n: list(range(m, 1 + n))
 
 
9,659

edits

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