0

I have such a formula:

enter image description here

I'm tryging to learn how to implement recursion in C, and wrote this code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int a(int n)
{
    if(n == 0)
        return 0;
    if(n == 1)
        return 1;
    if(n%2 == 0)
        return a(2*(n-1)) + a(2*(n-2));
    if(n%2 != 0)
        return a(2*n) - a(2*(n-1));
}

int main()
{
    int n = 3;
    printf("%d\n", a(n));

    return 0;
}

However, my code gives me segmentation fault, what's the problem?

7
  • Put printf("n = %d\n", n); at the beginning of the a function. Then run your program again and you will find out what happens. Commented Feb 18, 2016 at 14:05
  • 1
    I see the problem, but first a question: For the odd case, the image shows that the previous values are subtracted, not added. Your code (both versions), on the other hand, adds for both the even and the odd case. Which is correct? Do you want to subtract for the odd cases? This won't fix your segmentation fault, but the answer is needed to correct your code. Commented Feb 18, 2016 at 14:06
  • @TomKarzes: It's my mistake. I would like to implement the formula. I will edit the code. Commented Feb 18, 2016 at 14:07
  • The segfault is actually a stack overflow. Commented Feb 18, 2016 at 14:09
  • 1
    Why exactly do you need to use recursion? Is the program too fast or consuming too little memory? Commented Feb 18, 2016 at 14:15

3 Answers 3

3

You want to do this:

int a(int n)
{
    if(n == 0)
        return 0;
    if(n == 1)
        return 1;
    if(n%2 == 0)
        return a(n-1) + a(n-2);
    else
        return a(n-1) - a(n-2);
}

What you have there is (mathematically) called a sequence. Please take a look at the indices in your sequence then it probably makes more sense. You cannot just substitute 2n-1 there and FOR SURE not 2*(n-1) which is just wrong.

Furthermore: The recursion is not the best way to implement that if you want to expand your sequence into a series. Then you better should start with 0, 1 and do that stuff iteratively.

Hope that helps

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

4 Comments

This is correct. It's not as efficient as my version, but it works.
Your right. You should replace the != 1 case with an else like Tom Karzes did. I'll edit for future readers.
Sorry, I was confused. But for an optimizing compiler replacing the last condition with else will likely not make a difference.
Wont trust in that. If you can take some optimizing stuff off the compilers shoulder like that, you probably should think of it beforehand. I never "trust" in compilers optimizing everything.
2

Try this:

int a(int n)
{
    if(n == 0)
        return 0;
    if(n == 1)
        return 1;
    if(n%2 == 0)
        return a(n-1) + a(n-2);
    else
        return a(n-1) - a(n-2);
}

The problem is that the n in your function is not the same as the n in your image, which is why it's creating confusion. The thing to realize is that in the even case, you sum the previous two values, and in the odd case you subtract them, as shown above.

Comments

1

Your problem comes from indexes:

int a(int n)
{
    if(n == 0)
        return 0;
    if(n == 1)
        return 1;
    if(n%2 == 0)
        return a(n-1) + a(n-2);
    if(n%2 != 0)
        return a(n-1) - a(n-2);
}

Comments

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.