Recursion function (with bit shift)

Viewed 747
def recursion(x):
    if x == 0:
        return 0
    else:
        return x << 1 + recursion(x - 1)

print(recursion(3)) # 393216


print(3 << 1) # 6
print(2 << 1) # 4
print(1 << 1) # 2

In my head the output of the recursion function should be: 12 (6+4+2) Why is this not the case? I must say "393216" is slightly bigger than my expected number "12".

My expectation:

--> return 1<<1==2 for 1
--> return 2<<1==4 for 2
--> return 3<<1==6 for 3
0 --> return 0 for 0 

All together = 12

2 Answers
Related