zeta.c

来自「it is the Data Mining Algorithm source c」· C语言 代码 · 共 50 行

C
50
字号
/*----------------------------------------------------------------------  File    : zeta.c  Contents: compute Riemann's zeta-function  Author  : Christian Borgelt  History : 1998.10.20 file created            2008.03.14 main function added----------------------------------------------------------------------*/#ifdef ZETA_MAIN#include <stdio.h>#include <stdlib.h>#endif#include <math.h>#include "zeta.h"/*----------------------------------------------------------------------  Functions----------------------------------------------------------------------*/double zeta (double x){                               /* --- compute Riemann's zeta-function*/  double t = 1, z = 0;          /* term to add and result */  double base = 2;              /* base for terms */  do {                          /* compute the sum */    z += t;                     /* zeta(x) = \sum_{n=1}^\infty n^{-x} */    t = pow(base++, -x);        /* by successively adding terms */  } while (z +t > z);           /* until a terms gets zero */  return z;                     /* return the function value */}  /* zeta() *//*--------------------------------------------------------------------*/#ifdef ZETA_MAINint main (int argc, char *argv[]){                               /* --- main function */  double x;                     /* argument of the zeta function */  if (argc != 2) {              /* if wrong number of arguments given */    printf("usage: %s arg\n", argv[0]);    printf("compute Riemann's zeta-function for arg\n");    return 0;                   /* print a usage message */  }                             /* and abort the program */  x = atof(argv[1]);            /* get argument */  if (x <= 1) { printf("%s: x must be > 1\n", argv[0]); return -1; }  printf("%.16g\n", zeta(x));   /* compute zeta function */  return 0;                     /* return 'ok' */}  /* main() */#endif

⌨️ 快捷键说明

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