Tuesday, 13 August 2013

C Algo- Pow function - Without multiplication - 2 types of looping

C Algo- Pow function - Without multiplication - 2 types of looping

int pow(int Rn, int n, int r)
{ int i=0, temp_Rn=0;
if(n>1)
{ for(i=0;i<r;i++)
{
temp_Rn=temp_Rn+Rn;
}
temp_Rn=pow(temp_Rn,n-1,r);
return temp_Rn;
}
else
return Rn;
}
int pow(int Rn, int n, int r)
{
int i, temp_Rn=0;
bool flag_1=true;
if(n>1)
{ i=r;
while (flag_1)
{
switch(i)
{
case 0: temp_Rn=pow(temp_Rn,n-1,r);
flag_1=false ;
return temp_Rn;
default :temp_Rn=temp_Rn+Rn;
i--;
}
}
}
else
return Rn;
}
The above are two types of looping used in this sleek algo of mine, may i
please get a discussion on pros and cons as well as betterment
suggestions? It was made in Dev C++, ver 4.9. It is for r^n .. called as
pow(r,n,r); can be used as pow(r,n) with a little more modifications

No comments:

Post a Comment