fibo.c

来自「C语言库函数的源代码,是C语言学习参考的好文档。」· C语言 代码 · 共 49 行

C
49
字号
/* +++Date last modified: 05-Jul-1997 */

/*
    This program demonstrates how to calculate any arbitrary term
    of the Fibonacci sequence using phi (the golden number) and
    eighteenth century mathematician A. de Moivre's formula.

    written on Fri  04-05-1996  by Ed Beroset
        and released to the public domain by the author

    modified for SNIPPETS 01-Sep-1996 by Bob Stout
*/

#include "snipmath.h"                           /* includes math.h      */

double fibo(unsigned short term)
{
      const double k = 1/sqrt(5.0);             /* a handy constant     */
      double x;                                 /* the Fibonacci term   */

      /*
      **  this is de Moivre's formula using phi, the golden number (defined
      **  in snipmath.h), and as simplified by Donald Knuth
      */

      x = k * pow(PHI, term);

      /*
      **  this is to compensate for computer error
      */

      return dround(x);
}

#ifdef TEST

#include <stdio.h>

int main()
{
      unsigned i;                               /* which term?          */

      for (i = 1; i < 84; i++)                  /* limit of precision   */
            printf("%2d: %20.0f\n", i, fibo(i));
      return 0;
}

#endif /* TEST */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?