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 |
===Functional=== |
||
⚫ | |||
<lang python>def perf(n): |
|||
⚫ | |||
print ( |
|||
list(filter(perf, range(1, 10001))) |
|||
)</lang> |
|||
Or, |
Or, an order of magnitude faster (by restricting the search space): |
||
<lang python>from itertools import (chain) |
<lang python>from itertools import (chain) |