home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Professional
/
OS2PRO194.ISO
/
os2
/
prgramer
/
cenvi
/
fibonacc.cmm
< prev
next >
Wrap
Text File
|
1994-03-04
|
1KB
|
33 lines
//*****************************************************************
//*** Fibonacci.cmm - CMM code to print fibonacci numbers until ***
//*** ver.1 the user presses a key ***
//*****************************************************************
flush_keyboard() { while kbhit() getch() }
/******************************************************************************/
/*********************** ARRAY FIBONACCI METHOD *****************************/
/******************************************************************************/
printf("Printing Fibonacci sequence while creating an array until you press a key,\n")
printf("or until we run out of memory or stack space.\n")
for ( i = 0; !kbhit(); i++ )
printf("%d\t",FibArray[i] = (i < 2) ? i + 1 : FibArray[i-1] + FibArray[i-2])
flush_keyboard()
/******************************************************************************/
/********************* RECURSIVE FIBONACCI METHOD ***************************/
/******************************************************************************/
printf("\nPrinting Fibonacci sequence through recursion until you press a key,\n")
printf("or until we run out of memory or stack space.\n")
for ( i = 1; !kbhit(); i++ )
printf("%d\t",fib(i))
flush_keyboard()
fib(n) // return any number from fibonacci seqeunce, evaluating previous values
{ // recursively if necessary
return (n <= 2) ? n : fib(n-1) + fib(n-2)
}