0

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.

11
  • 7
    Are you excited about that ten or is that a factorial? Commented Mar 26, 2013 at 19:43
  • Hint: double f1Rek(int k, int x) Commented Mar 26, 2013 at 19:45
  • What do you mean with the ten? 1 <= x <= 10, do you mean the ten here? Commented Mar 26, 2013 at 19:47
  • yeah is it 1 <= x <= 10 or 1 <= x <= 3628800. Commented Mar 26, 2013 at 19:50
  • Beta can you explain it a little bit more please? Commented Mar 26, 2013 at 19:50

2 Answers 2

2

f(x)=sqrt(1^1+sqrt(2^2+ ( sqrt(3^3)+...+ ( sqrt(x^x))) )...

(Assuming what's in bold)

Equivalently,

f(x) = g(1, x)

where

g(i, n) = sqrt(i^i + g(i+1, n)) if i <= n, else 0

You should be able to elegantly code your recursive function from that.

In regard to a function with one argument:

f is a function with one argument. It calls a recursive helper function that takes two arguments. It's clear that the recursive inner function needs two arguments, one of which is the termination value, x. In a language with local functions that argument can be hoisted out of the function, e.g.,

double f(int x)
{
   double g(int i)
   {
       return (i <= x)? sqrt(pow(i, i) + g(i+1)) : 0.0;
   }

   return g(1);
}

GCC supports local functions, but they aren't in standard C.

Sign up to request clarification or add additional context in comments.

7 Comments

The first argument of g(1,x) should always the one?
Look again ... g(i+1, n) ... that's not 1. The initial first argument is 1, because that's how the series starts. The second argument is always n (the x argument of f) because that's the termination value.
@T.C. "The first argument of g(1,x) should always the one" Presented like that, how could it be anything else? I think you meant g(i,x), and Jim answered that.
Okay i get it.. it's another solution, but i tried it with only one argument.
Jim Balter, is the function not possible with only one argument?
|
1
double f1Rek(int x)
{  
   double res = 0.0;
   for(int i=x; i > 0; i--) {
     res = sqrt(power(i,i) + res));
   }
   return res;
}

2 Comments

If all you need is the right results, then it's useful ... and you should change your question. But if you're doing homework that is supposed to help you understand how to write recursive functions then it isn't useful.
It has nothing to do with homework.. i just try to programm mathematical in C, nothing else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.