Gracias, Wikipedia.
ShareCode
Permalink: http://www.treeweb.es/u/974/ 01/02/2011

ShareCode

1 //2 // Copyright (c) 2003-2013 Scalable Informatics3 //4 5  6 7  var i,j,k,milestone,n,rc;8  var NMAX=5000000, MMAX=10;9  var array,total,p_sum,sum,delta_t,pi, pi_exact, t_now;10  var inf=0 ;11  var name_length=0;12  var cpu_name,c;13  var caliper = new Array(10);14  var mod_getopt = require('posix-getopt');15  var parser, option;16  var os = require("os");17  var printf = require("printf");18  var out;19  20  milestone = 0;21  n = 0;22  sum = 0.0;23  pi_exact = 4.0*Math.atan(1.0);24  25  caliper[milestone] = new Date().getTime();26  27  /*28  Riemann zeta function of integer argument29  (c.f. http://mathworld.wolfram.com/RiemannZetaFunction.html )30  31  zeta(n) = sum[k=1;k< =inf;k++] (1.0/pow(k,n))32  33  inf 34  ----35  \ 136  zeta(n) = > -37  / n38  ---- k39  k=140  41 42  this code will compute zeta(2) in order to calculate43  pi. pi*pi/6 = zeta(2), so pi = sqrt(6*zeta(2))44 45  to run this code, type46 47  ./rzf.exe -l INFINITY -n n48 49  where INFINITY is an integer value of how many terms you would 50  like to take for your sum, and n is the argument to the Reimann51  zeta function. If you use 2 for n, then it will calculate pi 52  for you as well.53 54  */55  var opt;56  parser = new mod_getopt.BasicParser('n:l:', process.argv);57  while ((opt = parser.getopt()) !== undefined) {58  switch (opt.option) {59  case 'n':60  console.log("n option found. Argument is: " + opt.optarg);61  n = opt.optarg;62  break;63  case 'l':64  console.log("l option found. Argument is: " + opt.optarg);65  inf = opt.optarg;66  break;67  }68 }69  70  71 72  sum=0;73  74  cpu_name = os.hostname();75  out = printf("D: running on machine = %s\n",cpu_name);76  console.log(out);77  78  milestone++;79  caliper[milestone] = (new Date).getTime();80  81  82  for(k=inf-1;k>=1;k--)83  {84  sum += 1.0/Math.pow(k,n);85  }86 87  milestone++;88  caliper[milestone] = new Date().getTime();89  90  out = printf("zeta(%i) = %.15f \n",n,sum);91  console.log(out);92  if (n == 2)93  {94  pi = Math.sqrt(sum*6.0);95  out = printf("pi = %.15f \n",pi);96  console.log(out);97  98  out = printf("error in pi = %.15f \n",pi_exact-pi);99  console.log(out);100  out = printf("relative error in pi = %.15f \n",(pi_exact-pi)/pi_exact);101  console.log(out);102  }103  104  105  /* now report the milestone time differences */106  for (i=0;i<=(milestone-1);i++)107  {108  delta_t = (caliper[i+1] -caliper[i])/1000;109  110  out = printf("Milestone %i to %i: time = %fs\n",i,i+1,delta_t);111  console.log(out);112  }
Enlace
El enlace para compartir es: