I wrote a function to calculate the power of an integer then take modulus. I wonder if what I did is tail recursion :
int takeModulusTAILRECURSION(int coef, int x, int n, int module){
int result = 0;
if ( n > 0 )
result = takeModulusTAILRECURSION( mod(coef * x, module), x , n - 1, module );
else if ( n == 0)
result = mod ( coef , module );
return result;
}
//take modul of an integer ( both negative and positive )
int mod(int x, int modul){
while ( x < 0)
x += modul;
return x % modul;
}
Here is another function using recursion purely, which I think is different from the above :
int takeModulusNORMRECURSION(int x, int n, int module){
if ( n == 1 )
return mod( x, module );
else {
int total = x * takeModulusNORMRECURSION( x, n - 1, module);
return mod( total, module);
}
}
By the way, anyone can show me how to check the frame stack in Eclipse in these cases ? as I tried ( not know right or wrong) and they both take many stacks running.