Fibonacci Series

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 6
Reputation: kookai is an unknown quantity at this point 
Solved Threads: 0
kookai kookai is offline Offline
Newbie Poster

Fibonacci Series

 
0
  #1
Aug 2nd, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,341
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Fibonacci Series

 
0
  #2
Aug 2nd, 2006
A search for Fibonacci here or with Google ought to give you enough to start with, if not something to finish with.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: Fibonacci Series

 
0
  #3
Aug 3rd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Fibonacci Series

 
0
  #4
Aug 3rd, 2006
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 6
Reputation: kookai is an unknown quantity at this point 
Solved Threads: 0
kookai kookai is offline Offline
Newbie Poster

Re: Fibonacci Series

 
0
  #5
Aug 5th, 2006
G'day

where error?

1 error(s), 0 warning(s)



  1. # include <iostream.h>
  2. int fibonacci(int n)
  3. {<blockquote> int x1 = 0, fib;
  4. int x2 = 1;
  5. if(n >= 1)
  6. {<blockquote> for(int i=2;i<= n; i++)
  7. {
  8. </blockquote></blockquote><blockquote><blockquote><blockquote>fib = x1+ x2;
  9. x1 = x2;
  10. x2 = fib;
  11. </blockquote></blockquote></blockquote><blockquote><blockquote> }
  12.  
  13. return fib;
  14. </blockquote></blockquote><blockquote> }
  15. </blockquote>
Last edited by WolfPack; Aug 5th, 2006 at 7:58 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Fibonacci Series

 
1
  #6
Aug 5th, 2006
Try to append this to your code
  1. }
  2.  
  3. int main()
  4. {
  5. cout<<fibonacci(/*some number*/);
  6. }
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Fibonacci Series

 
1
  #7
Aug 5th, 2006
> where error?
Please use the code tags when posting code - can't you read this
http://www.daniweb.com/techtalkforum...cement8-3.html
Or see the background image in the compose message window?

Also, count the { and }
I count 3 { and only 2 }
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 6
Reputation: kookai is an unknown quantity at this point 
Solved Threads: 0
kookai kookai is offline Offline
Newbie Poster

Re: Fibonacci Series

 
0
  #8
Aug 5th, 2006
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int fibonacci(int n)
  5. {
  6. int x1 = 0, fib;
  7. int x2 = 1;
  8. if(n >= 1)
  9. {
  10. for(int i=2;i<= n; i++)
  11. {
  12. fib = x1+ x2;
  13. x1 = x2;
  14. x2 = fib;
  15. }}
  16.  
  17. return fib;
  18. }
  19. int main(){
  20. for(int i=2;i<=10;i++) {
  21. cout<<fibonacci(i)<<endl;
  22. }
  23. return 0;
  24. }

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
Last edited by Dave Sinkula; Aug 5th, 2006 at 3:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,341
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Fibonacci Series

 
0
  #9
Aug 5th, 2006
Originally Posted by kookai
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: asad jaan is an unknown quantity at this point 
Solved Threads: 0
asad jaan asad jaan is offline Offline
Newbie Poster

Re: Fibonacci Series

 
0
  #10
Mar 12th, 2009
Here is the shortest code for finding the fibo series

#include <iostream>
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;
}
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC