0,2,8,26,80,242,728,2186,6560,19682....how can i write these series with recursion?
if we start k=0 and then return the function 3k+2 we can get that but I don't know how to turn this into code,can you help me about that?

2. All recursion can be broken up into two pieces:
A - the "base case" when you've reached the "bottom" (or the "beginning")

B - every other case, where you call the function with a smaller value, then manipulate that value as necessary.

3. Could you be more specific?

4. Originally Posted by Figen
0,2,8,26,80,242,728,2186,6560,19682....how can i write these series with recursion?
if we start k=0 and then return the function 3k+2 we can get that but I don't know how to turn this into code,can you help me about that?
First of all, the mods here take a rather dim view of people using us to do their homework.

You really need to make your best effort on this, on your own, and if you run into problems post your code (in code tags please) and we'll see what we can do...

FWIW... you don't actually need recursion to solve this problem...

5. Mathematically
Code:
```f(k) = 3 * f(k-1) + 2;  k > 0
f(0) = 0;```
Edit: Just apply what tabstop said.

6. Originally Posted by CommonTater
FWIW... you don't actually need recursion to solve this problem...
I think the hook here is to do it with recursion. I think he would've come up with some other way to implement it if his job was just to implement that series.

7. Originally Posted by fronty
I think the hook here is to do it with recursion. I think he would've come up with some other way to implement it if his job was just to implement that series.
I figured it was an assignment for some course or another.

It's just that recursion is, to say the least, not the safest way of doing things where you have another choice on hand.

8. Originally Posted by Figen
Could you be more specific?
Nope, that's pretty much as fundamental as it gets. What do you do when n=0, and what do you do when n doesn't equal 0.

9. Okey then I will give a shot that here and please help me to improve it
Code:
```main()

{

int i,k=0;

printf("%d,",k);

recursion();

printf("%d,",k);

}

recursion()

{

for(i=0;i<13;i++) {

k=3k+2;

return k;

}```
Now how about some assitment now?

10. Recursion is when a function calls itself, meaning you enter function foo and foo also calls foo, unless the base case is true, which is the bottom. At the bottom, now you unwind all the way to the top, resolving all the statements that depended on previous calls of foo.

11. That's not recursion. Recursion is when a function calls itself. Your function is iterative and only calculates the value of the summation for k equal to 13. You don't calculate for k = 1, 2, ... 12. An example of recrusion would look like:
Code:
```void factorial(int x)
{
if (x == 0)  // base case, i.e. stop the recursion
return 1;
else
return x * factorial(x - 1);  // recurse by calling foo with some other value
}```
Put that together with tabstop's and Baying Naung's posts, and you should be able to come up with a solution.

12. This is the basic layout of a recursive function:

Code:
```#include <stdio.h>

void recurse(int n);

int main() {
int n;

n=0;
recurse(n);

(void) getchar();
return 0;
}
void recurse(int n) {
if (n>10)      //the base case,
return;
printf("n = %d\n", n);
recurse(n+1); //recursive calls

//you don't need to change your variables inside the parameter parenthesis ( )
//having n+= 1; and then recurse(n) is equivalent.
}```

13. Code:
```main()

{

int i,k=0;

printf("%d,",k);
for(i=0;i<13;i++){
recursion(k);
printf("%d,",k);
}

}

recursion(k)

{

k=3k+2;

return recursion(k);

}```