>>> sum(range(11))
55
>>> sum([i for i in range(1,11)])
55
>>> sum(i for i in range(1,11))
55
>>> import functools
>>> from functools import reduce
>>> reduce(lambda x,y: x + y, range(1,11))
55
>>> from operator import add
>>> reduce(add, range(1,11))
55
>>> def add_r(n):
if n == 0:
return 0
return n + add_r(n-1)
>>> add_r(10)
55
>>> def add_tr(n, r=0):
if n == 0:
return r
r += n
return add_tr(n-1, r)
>>> add_tr(10)
55
>>> s = 0
>>> for i in range(1, 11):
s += i
>>> s
55
Recommended Posts