# Thread: Fibonacci How should I make a generator

1. ## Fibonacci How should I make a generator

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

2. ## sigh

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

the binary conversion was a bonus..

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

4. ## 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
Look up the formula for calculating the nth term of the Fabonacci sequence and then use recursion.

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

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

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

8. An endless loop is
Code:
` for (; ;)`

9. Or for the smiley challenged :
Code:
```for ( ; ; )

or

while ( 1 )```
-Prelude

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

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

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