def power(x,n):
if n == 0:
return 1
elif n % 2 == 0:
return power(x * x, n % 2)
else:
return x * power(x, n - 1)
print power(2,3)
What ever the input I give it is showing either 1 or 2. can anybody please help me where I went wrong. I am newbie to programming. Thank you
Try changing this line:
return power(x * x, n % 2)
to this:
return power(x * x, n / 2)
if n is divisible by 2, you are calculating pow(x * x, 0)
which will always return 1.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments