0
Python: Write a recursive function to compute the sum of numbers from 1 to n. Provide the code that you used.
help me with my problem please
5 Answers
+ 1
#python 2 example 1
num = raw_input('your number: ')
print reduce(lambda x,y: x+y if x!= 1 else x, [n for n in xrange(num, 0, -1)])
#python 2 example 2
num = raw_input('your number: ')
print sum([x for x in xrange(num + 1)])
0
Language?
0
You can see "Recursive sum" in my code
0
def rec_sum(n):
if n == 1:
return n
return n + rec_sum(--n)