having trouble writing a recursive algorithm for 2^n
i am writing a recursive algorithm for the function 2^n, and the formula that must be used is: 2^(n-1) + 2^(n-1) = 2^n
heres what i have so far:
Code:
//computes 2^n recursively using the formula 2^n=2^n-1 + 2^n-1
x(int n)
{
if(n=0)
return 1;
else
}
i am not sure how to do the recursion to get that formula, can someone point me in the right direction? thanks
Re: having trouble writing a recursive algorithm for 2^n
Do you understand the concept of recursion? Your statement should include at least one call to the x() function.
Re: having trouble writing a recursive algorithm for 2^n
Quote:
Originally Posted by
kevinskrazyklub
i am writing a recursive algorithm for the function 2^n, and the formula that must be used is: 2^(n-1) + 2^(n-1) = 2^n
heres what i have so far:
Code:
//computes 2^n recursively using the formula 2^n=2^n-1 + 2^n-1
x(int n)
{
if(n=0)
return 1;
else
}
i am not sure how to do the recursion to get that formula, can someone point me in the right direction? thanks
Just think about it algebraically:
2^n = 2^(n-1) + 2^(n-1) = 2 * 2^(n-1)
x(n) = 2^n
x(n) = 2 * x(n-1)
Make sense? I haven't written any code and I still feel like I'm giving away the answer.
Re: having trouble writing a recursive algorithm for 2^n
Quote:
Originally Posted by
Hermit
Just think about it algebraically:
2^n = 2^(n-1) + 2^(n-1) = 2 * 2^(n-1)
x(n) = 2^n
x(n) = 2 * x(n-1)
Make sense? I haven't written any code and I still feel like I'm giving away the answer.
makes perfect sense thanks.