Fibonacci How should I make a generator

• 10-04-2002
cprog
Fibonacci How should I make a generator
How would a I make the loop so that it would keep generating it over nad over
• 10-04-2002
• 10-05-2002
PCV
sigh
Hey Cprog! Stop asking people here to do your homework for you.. hehe

the binary conversion was a bonus.. :cool:
• 10-05-2002
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.
• 10-05-2002
MrWizard
Re: Fibonacci How should I make a generator
Quote:

Originally posted by cprog
How would a I make the loop so that it would keep generating it over nad over

Look up the formula for calculating the nth term of the Fabonacci sequence and then use recursion.
• 10-05-2002
moi
Re: Re: Fibonacci How should I make a generator
Quote:

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
• 10-05-2002
Cshot
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...```
• 10-06-2002
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
• 10-06-2002
Max
An endless loop is
Code:

```  for (; ;)```
• 10-06-2002
Prelude
Or for the smiley challenged ;):
Code:

```for ( ; ; ) or while ( 1 )```
-Prelude
• 10-06-2002
MrWizard
Quote:

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.
• 10-06-2002
Prelude
>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
• 10-06-2002
Mister C
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