# Thread: Fibonacci Sequence

1. ## 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;
}```

2. Have you learned recursion? It would make this program a lot easier and cleaner.

3. Not yet, we are getting to it in 2 chapters

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

5. I realize it has to do with
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.

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

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

8. 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;
}```

9. Nope, this is all you need:

Code:
```int fib(int fib1, int fib2)
{return fib1 + fib2; }```

10. So after looking at some recursion tuts, can this be used for recursion, as dumb of a question as it may be.

11. Originally Posted by iGuardian
can this be used for recursion..
Well... if you mean .. recursion can be used for this.. the yes!

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

13. 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);```

14. Originally Posted by rmatze
We all had to learn just like you
So.. you have learnt everything ?.. :P

15. 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.
}```

Popular pages Recent additions