gerton
gerton

Reputation: 23

Fibonacci series

I am trying to do an exercise with the Fibonacci series. I have to implement with a recursive function, a succession of the prime n number of Fibonacci and print them in the same function. The problem is that my function print also the intermediate number. The results, for example, for n = 6, should be : 1 1 2 3 5 8. Any solutions?

Thanks

#include<iostream>
using namespace std;
int rec(int n)
{
    int a, b;
    if (n == 0 || n == 1)
    {
        return n;
    }
    else
    {
        a = rec(n - 1);
        b = rec(n - 2);
        cout << a + b << endl;
        return a + b;
    }

}
int main()
{ 
    int n = 6;
    rec(n);
    return 0;
}

Upvotes: 2

Views: 1271

Answers (5)

Damian
Damian

Reputation: 4641

Try this recursive function.

int fib(int n)
   return n<=2 ? n : fib(n-1) + fib(n-2);

It is the most elegant solution I know.

Upvotes: 0

Eric Gitangu
Eric Gitangu

Reputation: 1

I'm pretty sure you have gotten working solutions but I have a slightly different approach that doesn't require you to use data structures:

/* Author: Eric Gitangu Date: 07/29/2015 This program spits out the fibionacci sequence for the range of 32-bit numbers Assumption: all values are +ve ; thus unsigned int works here */

#include <iostream>
#include <math.h>
#define N pow(2.0,31.0)

using namespace std;

void fibionacci(unsigned int &fib, unsigned int &prevfib){
    unsigned int temp = prevfib;
    prevfib = fib;
    fib += temp;
}
void main(){
    int count = 0;
    unsigned int fib = 0u, prev = 1u;

    while(fib < N){
        if( fib ==0 ){
            fib = 0;
            cout<<" "<< fib++ <<" \n ";
            continue;
        }
        if( fib == 1 && count++ < 2 ){
            fib = 1;
            cout<< fib <<" \n ";
            continue;
        }
        fibionacci(fib, prev);
        cout<< fib <<" \n ";
    }
}

Upvotes: 0

Ali Akber
Ali Akber

Reputation: 3800

You can use this:

#include<iostream>
using namespace std;
#define MAXN 100
int visited[MAXN];
int rec(int n)
{
    if(visited[n])
    {
        return visited[n];
    }
    int a, b;
    if (n == 0|| n==1)
    {
        return n;
    }
    else
    {
        a = rec(n - 1);
        b = rec(n - 2);
        cout << " " <<a + b;
        return visited[n] = a + b;
    }

}
int main()
{
    int n = 6;
    cout<< "1";
    rec(n);
    cout<<endl;
    return 0;
}

This implementation uses dynamic programming. So it reduces the computation time :)

Upvotes: 1

user2736738
user2736738

Reputation: 30936

I have taken help of static int. That worked the way you wanted.

void rec(int n)
{
    static int a=0,b=1,sum;
    if(n>0)
    {
         sum = a+b;
         a=b;
         b= sum;
         cout<<sum<<" ";
         rec(n-1);
    }
}

Though you have to print the first Fibonacci number yourself in main().

cout<<"0 ";
rec(n);

Upvotes: 2

sam
sam

Reputation: 2033

Because you are printing in rec, its printing multiple times because of recursion. No need to print in the recursive function. Instead print the result in main

#include<iostream>
using namespace std;
int rec(int n)
{
    int a, b;
    if (n == 0 || n == 1)
    {
        return n;
    }
    else
    {
        a = rec(n - 1);
        b = rec(n - 2);
        //cout << a + b << endl;
        return a + b;
    }
}

int main()
{
    int n = 6;

    for (int i = 1; i <= n; i++)
    {
        cout << rec(i) << endl;
    }

    system("pause");
    return 0;
}

Upvotes: 0

Related Questions