+ 5

1+12+123+1234+....

Using recursion in Python

6th May 2020, 6:02 PM
Supreet Maurya
Supreet Maurya - avatar
10 Respostas
+ 11
try this def totalNum(n): n = [str(i) for i in range(1,n+1)] return int("".join(n)) def func(n): if num == 0: return 0 return totalNum(n) + func(n-1) # 123 + 12 + 1 print(func(3)) >>> 136 try understanding this code. hope you understand how to use recursion effectively.
6th May 2020, 11:10 PM
Shen Bapiro
Shen Bapiro - avatar
+ 10
How can i find the answer
6th May 2020, 7:59 PM
Pavithra Kanmanirajah
Pavithra Kanmanirajah - avatar
+ 2
Here is my C++ solution as a hint. You have to convert it into python code on your own 😉. https://code.sololearn.com/ca2RTcEtnFuL/?ref=app
6th May 2020, 8:04 PM
Michael
Michael - avatar
+ 1
Your attempt?show what you have done ,and we may help you further and mention the language name in tags where you have put o
6th May 2020, 6:03 PM
Abhay
Abhay - avatar
0
It needs to be a finite rather than an infinite addition.
7th May 2020, 11:06 PM
Sonic
Sonic - avatar
0
Up-to n terms means if the input is greater it may cause recursive error in python.
8th May 2020, 4:11 AM
Jenson Y
- 1
Upto n terms
6th May 2020, 6:07 PM
Supreet Maurya
Supreet Maurya - avatar
- 1
Yes by using recursive function
6th May 2020, 6:10 PM
Supreet Maurya
Supreet Maurya - avatar
- 1
a=0 n=int(input()) for i in range(1,n+1): a=a+int(str(i)*n) if n>0: n=n-1 print(a) Simplest code
8th May 2020, 5:35 PM
Rohit Gupta
Rohit Gupta - avatar
- 1
I said using recursion
8th May 2020, 5:36 PM
Supreet Maurya
Supreet Maurya - avatar