Perfect numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: Updated a primitive ( concatMap ))
Line 2,325: Line 2,325:
Or, a little faster (by restricting the search space):
Or, a little faster (by restricting the search space):


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




Line 2,345: Line 2,346:
lows + concatMap(
lows + concatMap(
lambda x: (
lambda x: (
lambda y=n / x: [y]
lambda y=(n / x): [y] if x != y else []
if x != y
else []
)()
)()
)(lows)
)(lows)
Line 2,358: Line 2,357:
# concatMap :: (a -> [b]) -> [a] -> [b]
# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
def concatMap(f):
def go(xs):
return lambda xs: list(
a = []
chain.from_iterable(
for x in xs:
map(f, xs)
a = a + f(x)
)
return a
)
return lambda xs: go(xs)




Line 2,371: Line 2,369:




if __name__ == '__main__':
main()</lang>
main()</lang>
{{Out}}
{{Out}}
<pre>[6, 28, 496, 8128]</pre>
<pre>[6, 28, 496, 8128]</pre>