Fibonacci How should I make a generator

This is a discussion on Fibonacci How should I make a generator within the C Programming forums, part of the General Programming Boards category; How would a I make the loop so that it would keep generating it over nad over Thanx in advance...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Cool Fibonacci How should I make a generator

    How would a I make the loop so that it would keep generating it over nad over
    Thanx in advance

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    The world is waiting. I must leave you now.

  3. #3
    PCV
    PCV is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    sigh

    Hey Cprog! Stop asking people here to do your homework for you.. hehe

    the binary conversion was a bonus..

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Homework question?
    Ah, I'll help...a little I guess.

    > How would a I make the loop so that it would keep generating it over nad over
    While your terminology confuses me, I'll take it you want to create and endless loop.

    Just create a loop that is based on a condition that will always be true.
    The world is waiting. I must leave you now.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: Fibonacci How should I make a generator

    Originally posted by cprog
    How would a I make the loop so that it would keep generating it over nad over
    Thanx in advance
    Look up the formula for calculating the nth term of the Fabonacci sequence and then use recursion.

  6. #6
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Re: Fibonacci How should I make a generator

    Originally posted by MrWizard
    Look up the formula for calculating the nth term of the Fabonacci sequence and then use recursion.
    in the case of the fibonacci series, this is a horribly inefficient method
    hello, internet!

  7. #7
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Yep, very very inefficient.

    >> How would a I make the loop so that it would keep generating it over nad over

    Here's a hint:

    Code:
    var1   var2
    -----   -----
    0        1
    1        2
    3        5
    8        13
    etc...
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >in the case of the fibonacci series, this is a horribly inefficient method
    Only if you do it the icky way:
    Code:
    /* The icky way */
    long fib ( int n )
    {
      if ( n == 0 )
        return 0;
      else if ( n == 1 )
        return 1;
      else
        return fib ( n - 1 ) + fib ( n - 2 );
    }
    It may be cute, but the size of the recursive tree is ridiculous for small values of n. Better to use iteration or memoization to avoid continuous recalculation.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    An endless loop is
    Code:
     for (; ;)

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Or for the smiley challenged :
    Code:
    for ( ; ; )
    
    or
    
    while ( 1 )
    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Prelude
    >in the case of the fibonacci series, this is a horribly inefficient method
    Only if you do it the icky way:
    Code:
    /* The icky way */
    long fib ( int n )
    {
      if ( n == 0 )
        return 0;
      else if ( n == 1 )
        return 1;
      else
        return fib ( n - 1 ) + fib ( n - 2 );
    }
    It may be cute, but the size of the recursive tree is ridiculous for small values of n. Better to use iteration or memoization to avoid continuous recalculation.

    -Prelude
    I believe if (n == 0) should return 1. The first two terms in the fibonacci sequence are 1, 1 right ? Anyways I know that way is pretty inefficient. The only reason I posted here is because I had an assignment to write the algorithm in assembly using recursion so I was just sharing one possible solution. I did not say it was efficient or the best.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The first two terms in the fibonacci sequence are 1, 1 right ?
    The correct sequence should be:

    0 1 1 2 3 5 8 13 21...

    Without zero we wouldn't be able to do most of the things we can mathematically, so we can't forget it.

    -Prelude
    My best code is written with the delete key.

  13. #13
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Prelude is correct -mathematically it starts at 0. However, most CS books start the sequence with 1 and 1 not 0 and 1. I prefer the math way starting at 0.

    Mr. C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to make a KenGen ( for a game tool )
    By lonewolfy in forum C# Programming
    Replies: 4
    Last Post: 03-28-2007, 08:23 AM
  2. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  3. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 10:53 AM
  4. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 01:05 AM
  5. 'functions' in make?
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 06-21-2003, 02:16 PM

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