Question: Recursive procedure of piecewise

Hi, I'm new to Maple and I have to produce a recursive procedure with the nomenclature Puis:=proc(X,n::integer) which calculates X^n with:

for n<0   ->   1/(X^(-n))

for n=0  ->   1

for n being an even integer   ->   X^(n/2) * X^(n/2)

for n being an odd integer   ->   X*X^(n-1)

The procedure I produced is:

Puis:=proc(X,n::integer)
option remember;
if n<0 then Puis(X,-n);
elif n=0 then 1;
elif rem(n,2,x)=0 then Puis(X,n);
else X*Puis(X,n-1);
end if;
end proc;

 

i don't have any eror up to this point, but when I try to evaluate, only Puis(4,0) works

Puis(4,-1);
Error, (in content) too many levels of recursion
Puis(4,0);
                               1
Puis(4,3);
Error, (in content) too many levels of recursion
Puis(4,4);
Error, (in content) too many levels of recursion

 

I was wondering what was wrong with my procedure, I changed the 1/(X^(-n)) with X^n and X^(n/2)*X^(n/2) for X^n because it is equivalent. I think I should put some sort of initial value to limit the recursion, but with that kind of function i really don't know how. I also tried with the X^(n/2)*X^(n/2) for the even numbers, but it says Puis expects its 2nd argument, n, to be of type integer, but received 1/2.

 

Please Wait...