Fibonacci

# include <iostream>
using namespace std;
int Fibonacci(int n)
/* Fibonacci: recursive version */
{
if(n<=0) return 0;
else if(n==1) return 1;
else
{
return Fibonacci(n-1)+Fibonacci(n-2);
}
}
int main()
{
int a;
cin>>a;
cout<<Fibonacci(a);
}

this program is let me the numper of Fibonacci after i set index

my Quss? is : i need print all numper to index
exam :
1,1,2,3,5,8...this Fibonacci
1,2,3,4,5,6...this index
user set 3 (example)
out put is 1,1,2
So, where is your problem?
PS: Learn english. I had to do it too.
closed account (D80DSL3A)
Your problem affords a great chance to contrast 2 basic approaches to calculating the Fibonnaci numbers.
Iterative (using a loop) vs recursive (your version).

With the recursive approach I couldn't see any way except to calculate each fib(n) from scratch and output the values in a loop. This involves making n calls to the recursive version of Fibonnaci(). If anyone can show a way to do the output as the function is run once please do.

With the iterative solution the value is built up directly, allowing the intermediate values of fib(n) to be output as fib(a) is being calculated once. This is much more efficient so an iterative approach seems better for this particular problem.

Here are both solution methods:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
using namespace std;

/* Fibonacci: recursive version */
int Fibonacci_R(int n)
{
	if(n<=0) return 0;
	else if(n==1) return 1;
	else return Fibonacci_R(n-1)+Fibonacci_R(n-2);	
}

// iterative version
int Fibonacci_I(int n)
{
	int fib[] = {0,1,1};
	for(int i=2; i<=n; i++)
	{
		fib[i%3] = fib[(i-1)%3] + fib[(i-2)%3];
		cout << "fib(" << i << ") = " << fib[i%3] << endl;
	}
	return fib[n%3];
}

int main(void)
{		
	int a;
	cout << "a = ";
	cin>>a;

	// calculate the fib(i) from scratch for each i <= a using your recursive function
	cout << endl << "From recursive function" << endl;
	for(int i=1; i<=a; ++i)
	        cout << "fib(" << i << ") = " << Fibonacci_R(i) << endl;
	cout << endl;

	// or calculate fib(a) once and output the intermediate results from the looping version
	cout << "From iterative function" << endl;
	Fibonacci_I(a);

	cout << endl;
	return 0;
}


@hanst99. I was able to understand his question. Try to be a little nicer here.
Last edited on
closed account (3hM2Nwbp)
I've always used the Fibonacci problem as an example of when a look-up table can be valuable as well. Most professors overlook this third solution though.
fun2code (325) thx thats true ...thx
Try to be a little nicer here.


my Quss? is : i need print all numper to index

If he writes stuff like that it's obvious he didn't even attempt checking his post for errors. Of course not everyone can speak or write perfect english, but -especially if your english is bad - at least checking for spelling is mandatory. And I doubt his english is so bad that he couldn't have expressed himself better than this.
Topic archived. No new replies allowed.