I have a function that finds the exponent but I am confused about the complexity of the function.
Function:
def expo(number, exponent):
if exponent == 0:
return 1
elif exponent % 2 == 0:
val = expo(number, exponent / 2)
return val * val
else:
return number * expo(number, exponent - 1)
I tried to calculate and draw a graph of the number of calculations according to the exponent and got this result:
Graph:
Exponent : Calculations:
1 : 2, 2 : 3, 3 : 4, 4 : 4, 5 : 5, 6 : 5, 7 : 6, 8 : 5, 9 : 6, 10 : 6, 11 : 7, 12 : 6, 13 : 7, 14 : 7, 15 : 8, 16 : 6, 17 : 7, 18 : 7, 19 : 8, 20 : 7, 21 : 8, 22 : 8, 23 : 9, 24 : 7, 25 : 8, 26 : 8, 27 : 9, 28 : 8, 29 : 9, 30 : 9
As you can see the number of calculations is oscillating, I think Big-O notation will not be linear or quadratic. I think it will be like a multiple degree polynomial with representation like
Am I right or I just have the wrong idea of O(n) notation?
