res=1;n=4; def fact(n,res): if n==1: return res; return fact(n-1,n*res) | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

res=1;n=4; def fact(n,res): if n==1: return res; return fact(n-1,n*res)

Here how the stack memory is used? I couldn't make out with the implementation

9th Jun 2020, 12:14 PM
Levi
Levi - avatar
2 Réponses
+ 4
See the below code for explanation https://code.sololearn.com/cwMGT9QV6zo2/?ref=app Do, check below code if you want to know how stack works https://code.sololearn.com/cO5a7FwkEPne/?ref=app
9th Jun 2020, 1:08 PM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
0
Kiran Deep Naidu can you help me with this...? **WITH A SAMPLE INPUT** Towers_of_hanoi_recursion. def hanoi(n,rod_from,rod_middle,rod_to): #when n-1 plates are placed in the final position if n==1: print('plate 1* from %s to %s'%(rod_from,rod_to)) return #moving n-1 plates off the largest one to be able to move the largest disc hanoi(n-1,rod_from,rod_to,rod_middle) #moving the actual largest one print('plate %s from %s to %s'%(n,rod_from,rod_to)) #placing n-1 plates on the top of the largest one (calling this function recursively) hanoi(n-1,rod_middle,rod_from,rod_to) hanoi(3,'A','B','C')
11th Jun 2020, 9:41 AM
Levi
Levi - avatar