Like Tree3Likes

Fibonacci Sequence

This is a discussion on Fibonacci Sequence within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by iGuardian I apologize, I'm only 8 weeks into the course so I may come off more dumb ...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iGuardian View Post
    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
    A common experience, I assure you.

  2. #17
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Quote Originally Posted by manasij7479 View Post
    So.. you have learnt everything ?.. :P
    I wish...far from it. ha

  3. #18
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by iGuardian View Post
    I'm following along the tutorial while i attempt to do it. Is this correct so far provided what you showed me
    If you wanted a recursive function, you're a bit off the mark..
    It is written like
    Code:
    long foo(int n) 
    { 
        if (n == 0) return 0;
        if (n == 1) return 1;
        return foo(n-1) + foo(n-2); 
    }
    

    But remember that it is very inefficient.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #19
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    I assume I made the same mistake as before. Is this in the right direction?
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int fib ( int n );
    // Function reference.
    
    
    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.
    
    
        do
        {
            int n = 1;
            fib (n) == fib (++n);
            if (fib(n) % 3 == 0 || fib(n) % 5 == 0)
            {
                sum_of_all_fibs += fib(n);
            }
        }
    
    
        while ( sum_of_all_fibs < 4000000 );
    }
    
    
    int fib ( int n )
    {
        int fib = 0;
        int fib1 = 0;
        int fib2 = 1;
        int sum_of_all_fibs;
    
    
        fib = fib1 + fib2;
        fib1 = fib2;
        fib2 = sum_of_all_fibs;
    
    
        return sum_of_all_fibs;
    }

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    To be honest, I wouldn't use a function for the fibonacci number here. At any given point during your loop you have the two previous fibonacci numbers and the next one is equal to their sum. Then it's a matter of shifting the previous one back into the previous previous variable, and moving the next one into the previous variable, ready for the next time around the loop.
    Then get your if statement in the right place, test the right variable, and loop up until the right ending condition.

    A recursive fibonacci function isn't just inefficient, it's diabolically inefficient at roughly O(2^(0.694n)). Even a value around 200 would take somewhere on the order of as much time as the age of the universe to calculate, on the worlds fastest PC. It simply cannot be used for this.
    The first thing that comes mind which is worse than that is Ackermann's function, which is saying a lot
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 2 of 2 FirstFirst 12
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, 10: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, 08:15 AM
  5. Fibonacci Sequence
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 09-08-2001, 11:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21