Thread: Fibonacci Sequence

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    30

    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;
    }
    Last edited by iGuardian; 10-20-2011 at 09:30 PM.

  2. #2
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Have you learned recursion? It would make this program a lot easier and cleaner.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    Not yet, we are getting to it in 2 chapters

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by rmatze View Post
    Have you learned recursion? It would make this program a lot easier and cleaner.
    ...but also (AFAIK) .. a lot less efficient.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    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. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    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. #7
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Quote Originally Posted by manasij7479 View Post
    ...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. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    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. #9
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Nope, this is all you need:

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

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    So after looking at some recursion tuts, can this be used for recursion, as dumb of a question as it may be.

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by iGuardian View Post
    can this be used for recursion..
    Well... if you mean .. recursion can be used for this.. the yes!

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    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. #13
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    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. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by rmatze View Post
    We all had to learn just like you
    So.. you have learnt everything ?.. :P

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    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 subscribe to a feed

Similar Threads

  1. Fibonacci sequence in c++
    By anytime in forum C++ Programming
    Replies: 50
    Last Post: 02-06-2011, 11:40 PM
  2. fibonacci sequence
    By cph in forum C Programming
    Replies: 57
    Last Post: 04-30-2009, 07:17 AM
  3. Fibonacci Sequence
    By Dogmasur in forum C Programming
    Replies: 15
    Last Post: 08-10-2008, 07:55 AM
  4. Fibonacci sequence
    By MuffanMan123 in forum C++ Programming
    Replies: 6
    Last Post: 02-26-2008, 09:15 AM
  5. Fibonacci Sequence
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 09-08-2001, 11:29 AM