A search for Fibonacci here or with Google ought to give you enough to start with, if not something to finish with.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
This is one way of going about it without much thinking
fn = [ Phi^ n - (-Phi)^-n ] / (2Phi-1)
for smaller values of n you need approximation.
Phi = ( 5^½ + 1 ) / 2 = 1.6180339 (the golden ratio)
^ = power
But this method is seldom used. This is more mathematical. Use Google as Dave suggested. You can get lots of material.
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
Simply put it this way, when ur applications demands a particular Fibonacci number (for eg. the 10th fibonacci number) then GOLDEN RATIO is the way to do it and if u require the whole series of numbers then the incremental method is the way.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
this is ok now I'm finish it but I need this
(1) Determine the largest int Fibonacci number that can be printed on your system.
(2) Modify the program of part (a) to use double instead of int to calculate and return Fibonacci numbers, and use this modified program to repeat part (b).
(1) Run the program. Use a value bigger than 10 in the main loop.
(2) Surely you can make an attempt at these changes.
Please enclose posted code in code tags -- just like it says in the little box you reply in.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>Here is the shortest code for finding the fibo series
I think this code is shorter, don't you?
void Fib(int fmax)
{
int i = 1, f[2] = { 0, 1 };
while (f[i] < fmax) {
cout << f[!i] << '\n';
f[i^=1] = *f + f[1];
}
}
;)
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Here is the shortest code for finding the fibo series
I also found a short code (nearly exactly the same as yours):
#include <iostream>
using namespace std;
int main(void)
{
int f0 = 0, f1 = 1, fn, repeat_times;
cout << "Enter number of elements (the first two elements are always displayed): ";
cin >> repeat_times;
cout << endl << "Fibbonacci series: 0 1 ";
for(int i = 0; i < repeat_times; i++)
{
fn = f0 + f1, f0 = f1, f1 = fn;
cout << fn << " ";
}
cout << endl;
return 0;
}
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
>Here is the shortest code for finding the fibo series
I think this code is shorter, don't you?
void Fib(int fmax)
{
int i = 1, f[2] = { 0, 1 };
while (f[i] < fmax) {
cout << f[!i] << '\n';
f[i^=1] = *f + f[1];
}
}
;)
The smallest and best optimized c++ code I ever saw for such a task !!!
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
>For integers, it will be just as accurate as the iterative or recursive version.
Okay heres the bottom line:
If you want to calculate the nth Fibonacci number, use the closed form(i.e. the formula mentioned in Post #3).
If you want to print the series till some n, always use the iterative version suggested by ArkM and other.
It may look that the closed form is always better than the iterative form since it will be calculated faster, but it is not. The calculation of power takes more time than simply to add/subtract hence the best choice( if printing the whole series) is the iterative version.
Both are of [tex]O(n)[/tex].
Recursive method in this case is evil. It is not suggested.
Now regarding the limits:
limit.h ( newly called climits ) header file contains the maximum value of int, float, double etc.
So, it is likely that you may run a loop till the current Fibonacci number is less than the INT_MAX (defined in climits).
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
Fibonacci in its recursive flavor:
int fib(unsigned n) {
if(n <= 1) return n;
else return fib(n-1) + fib(n-2);
}
( returns the nth element of the Fibonacci series )
Or even shorter:
int fib(unsigned n) {
return n <= 1 ? n : fib(n-1) + fib(n-2);
}
( returns the nth element of the Fibonacci series )
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243