How would a I make the loop so that it would keep generating it over nad over
Thanx in advance
Printable View
How would a I make the loop so that it would keep generating it over nad over
Thanx in advance
Hey Cprog! Stop asking people here to do your homework for you.. hehe
the binary conversion was a bonus.. :cool:
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.
Look up the formula for calculating the nth term of the Fabonacci sequence and then use recursion.Quote:
Originally posted by cprog
How would a I make the loop so that it would keep generating it over nad over
Thanx in advance
in the case of the fibonacci series, this is a horribly inefficient methodQuote:
Originally posted by MrWizard
Look up the formula for calculating the nth term of the Fabonacci sequence and then use recursion.
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...
>in the case of the fibonacci series, this is a horribly inefficient method
Only if you do it the icky way:
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.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 );
}
-Prelude
An endless loop is
Code:
for (; ;)
Or for the smiley challenged ;):
-PreludeCode:for ( ; ; )
or
while ( 1 )
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.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:
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.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 );
}
-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
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