home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FIBO.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  1KB  |  49 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4.     This program demonstrates how to calculate any arbitrary term
  5.     of the Fibonacci sequence using phi (the golden number) and
  6.     eighteenth century mathematician A. de Moivre's formula.
  7.  
  8.     written on Fri  04-05-1996  by Ed Beroset
  9.         and released to the public domain by the author
  10.  
  11.     modified for SNIPPETS 01-Sep-1996 by Bob Stout
  12. */
  13.  
  14. #include "snipmath.h"                           /* includes math.h      */
  15.  
  16. double fibo(unsigned short term)
  17. {
  18.       const double k = 1/sqrt(5.0);             /* a handy constant     */
  19.       double x;                                 /* the Fibonacci term   */
  20.  
  21.       /*
  22.       **  this is de Moivre's formula using phi, the golden number (defined
  23.       **  in snipmath.h), and as simplified by Donald Knuth
  24.       */
  25.  
  26.       x = k * pow(PHI, term);
  27.  
  28.       /*
  29.       **  this is to compensate for computer error
  30.       */
  31.  
  32.       return dround(x);
  33. }
  34.  
  35. #ifdef TEST
  36.  
  37. #include <stdio.h>
  38.  
  39. int main()
  40. {
  41.       unsigned i;                               /* which term?          */
  42.  
  43.       for (i = 1; i < 84; i++)                  /* limit of precision   */
  44.             printf("%2d: %20.0f\n", i, fibo(i));
  45.       return 0;
  46. }
  47.  
  48. #endif /* TEST */
  49.