[Solved] find `A` raised to `B`, then you need to multiply A to itself B number of times [closed]


def power(x,y):
   if y == 0:
    return 1
   a = power(x, y/2);
   if y%2 == 0:
    return a*a;
   else:
    return x*a*a;

x = 9
y = -8
if y < 0:
    print 1.0/(power(x,abs(y)))
else:
    print power(x,abs(y))

Keep it simple like this..

My pow() function calculates x^y when y is positive. If we want to find x^y for a negative y, then simply print 1/pow(x,y)

Note: @vivin: Does it answer correctly now?

Hope it helps!!!!

9

solved find `A` raised to `B`, then you need to multiply A to itself B number of times [closed]