Python: Write a recursive function to compute the sum of numbers from 1 to n. Provide the code that you used. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

19th Mar 2017, 7:50 AM
Peet Van Der Walt
Peet Van Der Walt - avatar
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)])
8th Aug 2017, 1:19 PM
David Katz
0
Language?
19th Mar 2017, 8:02 AM
Meharban Singh
Meharban Singh - avatar
19th Mar 2017, 8:08 AM
Peet Van Der Walt
Peet Van Der Walt - avatar
0
You can see "Recursive sum" in my code
19th Mar 2017, 8:22 AM
hdo
hdo - avatar
0
def rec_sum(n): if n == 1: return n return n + rec_sum(--n)
19th Mar 2017, 10:09 AM
Efi Shtainer
Efi Shtainer - avatar