Perfect numbers: Difference between revisions
Content added Content deleted
m (→{{header|Python}}: Minor updates for Python3) |
|||
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 |
<lang python>from functools import (reduce) |
||
from operator import (add) |
|||
from math import (sqrt) |
from math import (sqrt) |
||
Line 2,331: | Line 2,332: | ||
# main :: IO () |
# main :: IO () |
||
def main(): |
def main(): |
||
print( |
print( |
||
list(filter(perfect, enumFromTo(1)(10000))) |
|||
⚫ | |||
# perfect :: Int - > Bool |
# perfect :: Int - > Bool |
||
def perfect(n): |
def perfect(n): |
||
lows = filter( |
lows = list(filter( |
||
lambda x: 0 == (n % x), |
lambda x: 0 == (n % x), |
||
enumFromTo(1)(int(sqrt(n))) |
|||
) |
)) |
||
return (1 < n) and( |
return (1 < n) and( |
||
n == sum( |
n == sum( |
||
Line 2,355: | Line 2,358: | ||
# GENERIC -------------------------------- |
# GENERIC -------------------------------- |
||
# concatMap::(a - |
# concatMap :: (a -> [b]) -> [a] -> [b] |
||
def concatMap(f): |
def concatMap(f): |
||
def go(xs): |
|||
a = [] |
|||
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)) |
|||