954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Fibonacci Series

hi
I need help with this problem. I have tried writting the program, i just can not get it to run properly.

Please Write
The Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.
(a) Write a nonrecursive function fibonacci( n ) that calculates the nth Fibonacci number.

and than you

kookai
Newbie Poster
6 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
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
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

G'day

where error?:sad:

1 error(s), 0 warning(s)


# include <iostream.h>
int fibonacci(int n)
{[INDENT] int x1 = 0, fib;
int x2 = 1;
if(n >= 1)
{[INDENT] for(int i=2;i<= n; i++)
{
[/INDENT][/INDENT][INDENT][INDENT][INDENT]fib = x1+ x2;
 x1 = x2;
x2 = fib;
[/INDENT][/INDENT][/INDENT][INDENT][INDENT] }

return fib;
[/INDENT][/INDENT][INDENT] }
[/INDENT]
kookai
Newbie Poster
6 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

Try to append this to your code

}

int main()
{
    cout<<fibonacci(/*some number*/);
}
Grunt
Junior Poster
152 posts since Jul 2006
Reputation Points: 197
Solved Threads: 12
 

> where error?
Please use the code tags when posting code - can't you read this
http://www.daniweb.com/techtalkforums/announcement8-3.html
Or see the background image in the compose message window?

Also, count the { and }
I count 3 { and only 2 }

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
#include<iostream>
using namespace std;
 
int fibonacci(int n)
{
int x1 = 0, fib;
int x2 = 1;
if(n >= 1)
{
for(int i=2;i<= n; i++)
{
fib = x1+ x2;
x1 = x2;
x2 = fib;
}}
 
return fib;
}
int main(){
for(int i=2;i<=10;i++) {
cout<<fibonacci(i)<<endl;
}
return 0;
}


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).

and

kookai
Newbie Poster
6 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 
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
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Here is the shortest code for finding the fibo series

#include
using namespace std;

int main(){
long a;
cout << "Enetr Limit you want\n\n";
cin >> a;
cout << "Fibonachi numbers < " << a << endl;
long f = 0, f2, f1 = 1;
while(true) {
f2 = f + f1;
if(f2 > a) {
break;
}
cout << " " << f2 << endl;
f = f1;
f1 = f2;
}
system("pause");
return 0;
}

asad jaan
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

>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
 

A commen misunderstanding is that the closed form version,
is an approximation.

For integers, it will be just as accurate as the iterative or recursive version.

monkey_king
Junior Poster
160 posts since Aug 2008
Reputation Points: 70
Solved Threads: 9
 

>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
 
The smallest and best optimized c++ code I ever saw for such a task !!!


but i copy paste it and encountered on error (unresolved external '_main')

im using borlan c++ may b thats y...........


but i tried making my own program for Fibonacci series and it worked
i m a beginner. i thought array will will best suite this problem.....
so here is my program and i think THIS ONE IS THE SHORTEST :cool: if not then its realllly realllly easy............
so feel free to replay and tell me how was it

#include <iostream>
#include <conio.h>

void main(void)
{
int i,a[20];
a[0]=0;
a[1]=1;


for (i=0 ; i<=20 ; i++)
{
a[i+2]=a[i]+a[i+1];
}

for (i=0 ; i<=20 ; i++)
{
 cout<<a[i]<<"\n";

}
getch();
}

im using borland c++ 5.02 (old version ..ya but its gud :$)

kofchamp
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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
 

try out this code this is more easy then above

#include<iostream.h>
#include<conio.h>
void main()
{
   clrscr();
   int tf=0,i,s=1,n,f,fib=0;
   cout<<"Enter the total no of terms in series\n";
   cin>>n;
   for(i=0;i<n;++i)
   {
     fib=tf+s;
     cout<<tf<<" ";
     tf=s;
     s=fib;
   }
getch();
}
shubham.angrish
Newbie Poster
2 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

the largest value depends upown on your variable range.

shubham.angrish
Newbie Poster
2 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,fb,a=0,b=1;
cout<<"enter a number"; 
cin>>fb;
cout<<b;
for(i=1;i<fb;i++)
{
sum=a+b;
cout<<sum;
a=b;
b=sum;
}
getch();
}
tushar646
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You