Perfect numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: Updated a primitive ( concatMap ))
(→‎{{header|Python}}: Updated older functional version to work with Python 3 and generate output)
Line 2,313: Line 2,313:


=={{header|Python}}==
=={{header|Python}}==
===Procedural===
<lang python>def perf(n):
<lang python>def perf(n):
sum = 0
sum = 0
Line 2,319: Line 2,320:
sum += i
sum += i
return sum == n</lang>
return sum == n</lang>

Functional style:
===Functional===
<lang python>perf = lambda n: n == sum(i for i in xrange(1, n) if n % i == 0)</lang>
<lang python>def perf(n):
return n == sum(i for i in range(1, n) if n % i == 0)

print (
list(filter(perf, range(1, 10001)))
)</lang>




Or, a little faster (by restricting the search space):
Or, an order of magnitude faster (by restricting the search space):


<lang python>from itertools import (chain)
<lang python>from itertools import (chain)