I want to program the mathematical function f(x)=sqrt(1^1+sqrt(2^2+sqrt(3^3)+...+sqrt(x^x))), where x should be 1 <= x <= 10. I tried to programm the function like this:
double f1Rek(int x)
{
if( x < 1 ) return sqrt(power(x,x));
return sqrt(power(x,x) + f1Rek(x-1));
}
The function power is also a self created recursive function:
double power(int x, int n)
{
if( n == 0 ) return 1.0;
if( x == 0 ) return 0.0;
if( exp > 0 )
{
return n * power(n, exp - 1);
}
if( exp < 0 )
{
return 1 / ( n * power(n, -(exp-1));
}
}
The problem is the f1Rek(int x) function, because it starts with the sqrt(x^x + sqrt( x-1^x-1... . How can I solve the problem in a very elegant way?
Update:
With the answer of Jim Balter, i created a function with 2 Arguments:
double f2Rek(int i, int x)
{
if( i <= x )
{
return sqrt(power(i,i) + f2Rek(i+1, x));
}
else return 0.0;
}
Is there a possibility to define a recursive function with only one argument.
double f1Rek(int k, int x)