LoginSignup
0
0

More than 5 years have passed since last update.

Sum from 1 to 10

Last updated at Posted at 2016-03-11
>>> 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
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0