Perfect numbers: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
→‎Functional Python (faster version): A simplification, with one less import.
Hout (talk | contribs)
→‎Functional Python: Simplifying time compression. Updated relative time comment in preamble.
Line 2,330:
 
 
Or, over 20X50X faster, as measured by ''time.time()'':
 
<lang python>'''Perfect numbers'''
Line 2,340:
def perfect(n):
'''Is n the sum of its proper divisors other than 1 ?'''
 
# p :: Int -> Bool
def p(x):
'Factor of n ?'
return 0 == (n % x)
 
root = sqrt(n)
lows = list(filter(p,[x for x in enumFromTo(1)(int(root)) if 0 == (n % x))]
return 1 < n and (
n == sum(lows + [n / x for x in lows if root != x]) / 2
Line 2,356 ⟶ 2,351:
def main():
'''Test'''
 
print(
list(filterprint([
x for x in enumFromTo(1)(10000) if perfect,(x)
])
enumFromTo(1)(10000)
))
)