0
Can't understand the math . Why not 5 ?
2 ответов
+ 5
def recur(n):
if n == 0:
return 0
else:
return n + recur(n-1)
print(recur(3))
so let's start
n is 3 so it will return
return 3 + recur(2)
we can find the value of recur(2)
n = 2
return 2 + recur (1)
the above result become
return 3 + 2 + recur(1)
again for recur(1)
return 1 + recur(0)
since recur(0) will be 0 because n==0
the whole thing can be written as
return 3+2+1+0
which is 6
If you want to stop at 5
you can change n==0 to n==1
0
thanks a lot