1+12+123+1234+.... | Sololearn: Learn to code for FREE!



Using recursion in Python


5/6/2020 6:02:13 PM

Supreet Maurya

16 Answers

New Answer


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.


How can i find the answer


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


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


It needs to be a finite rather than an infinite addition.


Up-to n terms means if the input is greater it may cause recursive error in python.


Upto n terms


Yes by using recursive function


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


I said using recursion


Hadd hai