# Fibonacci Sequence

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-20-2011
iGuardian
Fibonacci Sequence
It compiles properly but when I run the program it is a blank screen. Is it because i do not call int fib( int n )?
"Edit"
I realize it's timing out now, I just do not understand why.
Code:

```#include <iostream> using namespace std; int fib ( int n ); int main() {     int total = 0;     int fibn = 0;     for ( int i = 0; i < 4000000; i++ )     {     fibn = fib(i);         if (fibn % 3 == 0 || fibn % 5 == 0)         {           total += fibn;         }     }     cout << "Your fibonacci number will be: ";     cout << total << "." << endl;     cout << "Thank you for using the program." << endl; } int fib ( int n ) {     if ( n == 0 || n == 1 )     return n;     int fib1 = 0;     int fib2 = 1;     int fib = 0;     for ( int i = 2; i < n; i++ )     {         fib = fib1 + fib2;         fib1 = fib2;         fib2 = fib;     }     return fib; }```
• 10-20-2011
rmatze
Have you learned recursion? It would make this program a lot easier and cleaner.
• 10-20-2011
iGuardian
Not yet, we are getting to it in 2 chapters
• 10-20-2011
manasij7479
Quote:

Originally Posted by rmatze
Have you learned recursion? It would make this program a lot easier and cleaner.

...but also (AFAIK) .. a lot less efficient.
• 10-20-2011
iGuardian
I realize it has to do with
Quote:

for ( int i = 0; i < 4000000; i++ )
but my question is how would I go about setting it to add all numbers that can be modded by 3 and or 5 that are less then 4 million.
• 10-20-2011
rmatze
You are making this more difficult then it has to be.

You are going to want to start with fib1 = 0 and fib2 = 1. Then create a loop. Call the fib function which all you want it to do is add two numbers and return that result...that is it. After returning the result put fib2 into fib1 and result into fib2. Print the number and continue with the loop calling the fib function.
• 10-20-2011
rmatze
Quote:

Originally Posted by manasij7479
...but also (AFAIK) .. a lot less efficient.

True, now that I've looked at it more, a simple do while loop will work just fine.
• 10-20-2011
iGuardian
so in this case, the function would look like
Code:

```int fib ( int n ){     int fib = 0;     int fib1 = 0;     int fib2 = 1;     fib = fib1 + fib2;     fib1 = fib2;     fib2 = fib;     return fib; }```
• 10-20-2011
rmatze
Nope, this is all you need:

Code:

```int fib(int fib1, int fib2) {return fib1 + fib2; }```
• 10-20-2011
iGuardian
So after looking at some recursion tuts, can this be used for recursion, as dumb of a question as it may be.
• 10-20-2011
manasij7479
Quote:

Originally Posted by iGuardian
can this be used for recursion..

Well... if you mean .. recursion can be used for this.. the yes!
• 10-20-2011
iGuardian
I apologize, I'm only 8 weeks into the course so I may come off more dumb than I really am, C++ isn't coming to me as easy as i thought
• 10-20-2011
rmatze
We all had to learn just like you. I think you should stay away from recursion. Just use a do while loop instead of the for loop.

Code:

```do     {         //call your fib function and save the return value into say result         //set fib1 equal to fib2         //set fib2 equal to result                 //print result             }while(result < 4000000);```
• 10-20-2011
manasij7479
Quote:

Originally Posted by rmatze
We all had to learn just like you

So.. you have learnt everything ?.. :D :P
• 10-20-2011
iGuardian
I'm following along the tutorial while i attempt to do it. Is this correct so far provided what you showed me
Code:

```#include <iostream> using namespace std; // Recursion will be used for this program. int fib ( int fib1, int fib2 )     {     return fib1+fib2;     }     // The function calls itself until the condition is met. int main() {     int sum_of_all_fibs = 0;     // The sum will begin at zero, this will be the final output of all numbers     // found to be modded by 5 or 3 and be less then 4,000,000. }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last