|
-
January 22nd, 2009, 09:57 PM
#1
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
-
January 22nd, 2009, 10:33 PM
#2
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.
-
January 22nd, 2009, 11:01 PM
#3
Re: having trouble writing a recursive algorithm for 2^n
 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.
- Alon
-
January 23rd, 2009, 12:28 AM
#4
Re: having trouble writing a recursive algorithm for 2^n
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|