I have the code
[code=python]
def calculation(n):
print 'n =', n
if n<=1:
return n
if n%2==0:
print 'Even'
return calculation(n-1) + calculation(n-2) + 1
else:
print 'Odd'
return n
print calculation(4)
[/code]
the result is
[code=python]
n = 4
Even
n = 3
Odd
n = 2
Even
n = 1
n = 0
6
[/code]
Can anyone explain why I get this result?
And provide some other similar examples, so I can get practiced with this kind of problem?
[code=python]
def calculation(n):
print 'n =', n
if n<=1:
return n
if n%2==0:
print 'Even'
return calculation(n-1) + calculation(n-2) + 1
else:
print 'Odd'
return n
print calculation(4)
[/code]
the result is
[code=python]
n = 4
Even
n = 3
Odd
n = 2
Even
n = 1
n = 0
6
[/code]
Can anyone explain why I get this result?
And provide some other similar examples, so I can get practiced with this kind of problem?
Comment