what is the definite formula for an exponent?

59 Views Asked by At

I'm attempting to code math formulas from square 1 where i use addition to make subtraction, multiplication, division, -etc. etc. But that brung me to exponents, i felt it was simple enough, and i smacked in a repeat and a self multiplying script, and i got what i was looking for, but when i got to things like 2^0, i got 0 because variable=(2)*(2) happening 0 times = 0 and not the usual 1, then negatives started becoming an issue on the scripts complexity, while i'd like to just fix them with more scripting, i want to find the definite formula for this so as to keep things simple.

though what is this formula?

2

There are 2 best solutions below

3
On

Just start the recursion at 0, eg :

def expo(x, y): """Computes x^y for integers x and y"""

if y==0:

return 1

elif y < 0:

"""thank you @Soham Saha for showing how to easily extend this algorithm for possibly negative $y$"""

return 1/expo(x, -y) 

else:

return x * expo(x, y-1)
0
On

The only "definite" formula I can think of is the power series of x^y (the one as a function of $x$ and the other as a function of $y$).