黎曼zeta函数的快捷c++实现

const double bn[130]={0,0.16666666666666665741,-0.033333333333333332871,0.023809523809523808202,-0.033333333333333332871,0.075757575757575759678,-0.25311355311355310249,1.1666666666666667407,-7.0921568627450977118,54.971177944862155584,-529.12424242424242493,6192.1231884057970092,-86580.253113553117146,1425517.1666666667443,-27298231.067816093564,601580873.90064239502,-15116315767.092157364,429614643061.16668701,-13711655205088.332031,488332318973593.1875,-19296579341940068.,841693047573682560.,-40338071854059454464.,2.1150748638081992622e+021,-1.2086626522296526202e+023,7.500866746076964166e+024,-5.0387781014810688499e+026,3.6528776484818122276e+028,-2.8498769302450882361e+030,2.3865427499683627448e+032,-2.1399949257225334859e+034,2.050097572347809739e+036,-2.0938005911346379301e+038,2.2752696488463514863e+040,-2.6257710286239577207e+042,3.2125082102718031743e+044,-4.1598278166794711978e+046,5.6920695482035283174e+048,-8.2183629419784577665e+050,1.2502904327166994004e+053,-2.0015583233248370052e+055,3.3674982915364375556e+057,-5.9470970503135450205e+059,1.1011910323627976762e+062,-2.1355259545253502079e+064,4.3328896986641193847e+066,-9.1885528241669331811e+068,2.0346896776329073708e+071,-4.7003833958035730158e+073,1.1318043445484249411e+076,-2.8382249570693707354e+078,7.4064248979678852935e+080,-2.0096454802756605262e+083,5.6657170050805942089e+085,-1.6584511154136215904e+088,5.036885995049237839e+090,-1.586146823765818563e+093,5.1756743617545625189e+095,-1.7488921840217115846e+098,6.1160519994952182359e+100,-2.2122776912707833194e+103,8.2722776798770968766e+105,-3.1958925111415708478e+108,1.275008222338779314e+111,-5.2500923086774131347e+113,2.2301817894241626634e+116,-9.7684521930955207422e+118,4.4098361978452949821e+121,-2.0508570886464088957e+124,9.8214433279791276581e+126,-4.8412600798208880569e+129,2.455308880148098232e+132,-1.2806926804084747554e+135,6.8676167104668579435e+137,-3.7846468581969105852e+140,2.1426101250665290651e+143,-1.2456727137183695399e+146,7.4345787551000155753e+148,-4.5535795304641703729e+151,2.861211281685887046e+154,-1.8437723552033869952e+157,1.2181154536221046544e+160,-8.2482187185314121679e+162,5.7225877937832942168e+165,-4.066853052505910459e+168,2.9596092064642052191e+171,-2.2049522565189457143e+174,1.6812597072889599375e+177,-1.3116736213556958091e+180,1.0467894009478038522e+183,-8.5432893578833710248e+185,7.1287821322486546855e+188,-6.0802931455535904967e+191,5.2996776424849921167e+194,-4.719425916874586047e+197,4.2928413791402982607e+200,-3.9876744968232205368e+203,3.7819780419358879766e+206,-3.6614233683681191899e+209,3.6176090272372859917e+212,-3.6470772645191356311e+215,3.7508755436454406323e+218,-3.934586729643903006e+221,4.2088211148190083957e+224,-4.5902296220617917543e+227,5.1031725772629571907e+230,-5.7822762303656954206e+233,6.6762482167835883802e+236,-7.8535307644450417022e+239,9.4106894067058724292e+242,-1.1484933873465184661e+246,1.4272958742848784918e+249,-1.8059559586909309354e+252,2.3261535307660806786e+255,-3.0495751715499594099e+258,4.0685806076433975665e+261,-5.5231031321974362007e+264,7.627727939643439515e+267,-1.0715571119697885792e+271,1.5310200895969188051e+274,-2.2244891682179835734e+277,3.2862679190690139906e+280,-4.9355928955960347988e+283,7.5349571200832510901e+286,-1.1691485154584178283e+290,1.8435261467838938419e+293,-2.9536826172968081795e+296,4.8079321277501568079e+299,-7.9502125045885251673e+302,1.335278418735463406e+306}
;const double fc[171]={0,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800.,87178291200.,1307674368000.,20922789888000.,355687428096000.,6402373705728000.,121645100408832000.,2432902008176640000.,51090942171709440000.,1.12400072777760768e+021,2.5852016738884978213e+022,6.2044840173323941e+023,1.5511210043330986055e+025,4.0329146112660565032e+026,1.088886945041835194e+028,3.0488834461171387192e+029,8.8417619937397018986e+030,2.6525285981219106822e+032,8.2228386541779224302e+033,2.6313083693369351777e+035,8.6833176188118859387e+036,2.9523279903960415733e+038,1.0333147966386145431e+040,3.7199332678990125486e+041,1.3763753091226345579e+043,5.2302261746660111714e+044,2.0397882081197444123e+046,8.159152832478976838e+047,3.3452526613163807956e+049,1.4050061177528799549e+051,6.0415263063373834074e+052,2.6582715747884488694e+054,1.1962222086548018857e+056,5.5026221598120891536e+057,2.5862324151116817767e+059,1.2413915592536072528e+061,6.0828186403426752249e+062,3.0414093201713375576e+064,1.5511187532873821895e+066,8.0658175170943876846e+067,4.2748832840600254848e+069,2.3084369733924137924e+071,1.2696403353658276447e+073,7.1099858780486348103e+074,4.05269195048772141e+076,2.3505613312828784949e+078,1.3868311854568983861e+080,8.3209871127413898951e+081,5.0758021387722483583e+083,3.146997326038793939e+085,1.982608315404440085e+087,1.2688693218588416544e+089,8.2476505920824715167e+090,5.4434493907744306945e+092,3.6471110918188683221e+094,2.480035542436830548e+096,1.7112245242814129738e+098,1.1978571669969892213e+100,8.5047858856786230047e+101,6.1234458376886084639e+103,4.4701154615126843855e+105,3.3078854415193862416e+107,2.4809140811395399745e+109,1.8854947016660503806e+111,1.451830920282858721e+113,1.1324281178206296794e+115,8.9461821307829757136e+116,7.1569457046263805709e+118,5.7971260207473678414e+120,4.7536433370128420198e+122,3.9455239697206587884e+124,3.3142401345653531943e+126,2.817104114380550131e+128,2.4227095383672734128e+130,2.1077572983795278544e+132,1.8548264225739843605e+134,1.6507955160908460244e+136,1.4857159644817615149e+138,1.3520015276784029158e+140,1.2438414054641308179e+142,1.1567725070816415659e+144,1.0873661566567430754e+146,1.0329978488239059305e+148,9.9167793487094964784e+149,9.6192759682482120384e+151,9.4268904488832479837e+153,9.3326215443944153252e+155,9.3326215443944150966e+157,9.4259477598383598816e+159,9.6144667150351270793e+161,9.9029007164861804721e+163,1.02990167451456281e+166,1.0813967582402909767e+168,1.1462805637347083683e+170,1.226520203196138005e+172,1.3246418194518290179e+174,1.4438595832024936625e+176,1.5882455415227430287e+178,1.7629525510902445874e+180,1.9745068572210740115e+182,2.2311927486598137657e+184,2.5435597334721876552e+186,2.9250936934930159967e+188,3.3931086844518980862e+190,3.9699371608087210616e+192,4.6845258497542909237e+194,5.5745857612076058231e+196,6.6895029134491271205e+198,8.094298525273444092e+200,9.875044200833601058e+202,1.2146304367025329301e+205,1.5061417415111409314e+207,1.8826771768889261129e+209,2.3721732428800468512e+211,3.0126600184576594309e+213,3.8562048236258040716e+215,4.974504222477287459e+217,6.4668554892204741474e+219,8.4715806908788206314e+221,1.1182486511960043298e+224,1.4872707060906857134e+226,1.9929427461615187928e+228,2.6904727073180504073e+230,3.6590428819525488642e+232,5.0128887482749919605e+234,6.9177864726194885808e+236,9.6157231969410893532e+238,1.3462012475717525742e+241,1.8981437590761708898e+243,2.695364137888162853e+245,3.8543707171800730787e+247,5.5502938327393044385e+249,8.0479260574719917061e+251,1.1749972043909107097e+254,1.727245890454638923e+256,2.5563239178728653927e+258,3.8089226376305697893e+260,5.713383956445854684e+262,8.6272097742332399855e+264,1.3113358856834524492e+267,2.0063439050956822953e+269,3.0897696138473507759e+271,4.789142901463394078e+273,7.4710629262828942235e+275,1.1729568794264144743e+278,1.853271869493734989e+280,2.9467022724950384028e+282,4.7147236359920616095e+284,7.5907050539472189932e+286,1.2296942187394494177e+289,2.0044015765453026266e+291,3.2872185855342959088e+293,5.423910666131588675e+295,9.0036917057784375454e+297,1.5036165148649991456e+300,2.5260757449731984219e+302,4.2690680090047051083e+304,7.257415615307999035e+306};

double zps(double s,double sc){
	int n = int(double(log(0.00001) / -2));
   double sum = 0;
   double two_n = ldexp(double(1), n);
   int ej_sign = 1;
   for(int j = 0; j < n; ++j)
   {
      sum += ej_sign * -two_n / pow(double(j + 1), s);
      ej_sign = -ej_sign; 
   }
   double ej_sum = 1;
   double ej_term = 1;
   for(int j = n; j <= 2 * n - 1; ++j)
   {
      sum += ej_sign * (ej_sum - two_n) / pow(double(j + 1), s);
      ej_sign = -ej_sign;
      ej_term *= 2 * n - j;
      ej_term /= j - n + 1;
      ej_sum += ej_term;
   }
   return -sum / (two_n * (-(pow(double(2), sc)-1)));
}
double zip(double s,double sc){
	double result;
   if(s >= 10000)
      return 1;
   result = zps(s, sc); 
   return result;
}
double zoi(double s,double sc){
   static double results[50] = {};

      for(unsigned k = 0; k < sizeof(results) / sizeof(results[0]); ++k)
      {
         double arg = k * 2 + 3;
         double c_arg = 1 - arg;
         results[k] = zps(arg, c_arg);
      }
   
   unsigned index = (s - 3) / 2;
   return index >= sizeof(results) / sizeof(results[0]) ? zps(double(s), sc): results[index];
}
double zi(double s, double sc)
{
	if(s==0) return 0.5;
   if(sc == 0)
      return 1./0.;
   double result;
   if(s > 10000)
      return 1;
   
   if(floor(s) == s)
   {
      try
      {
         int v = int(s);
         if(v == s)
         {
            if(v < 0)
            {
               if(((-v) & 1) == 0)
                  return 0;
               int n = (-v + 1) / 2;
               if(n <= 129)
                  return double((-v & 1) ? -1 : 1) * bn[n] / (1 - v);
            }
            else if((v & 1) == 0)
            {
               if(((v / 2) <= 129) && (v <= 170))
                  return double(((v / 2 - 1) & 1) ? -1 : 1) * ldexp(double(1), v - 1) * pow(3.14159265358979323, v) *
                     bn[v/2] / fc[v];
               return double(((v / 2 - 1) & 1) ? -1 : 1) * ldexp(double(1), v - 1) * pow(3.14159265358979323, v) *
                  bn[v/2] / fc[v];
            }
            else
               return zoi((double)v, sc);
         }
      }
      catch(...){} 
   }

   if(fabs(s) < 0.00001)
   {
      result = -0.5f - 0.91893853320467278056 * s;
   }
   else if(s < 0)
   {
      double temp=s;
      s=sc;
      sc=temp;
      if(floor(sc/2) == sc/2)
         result = 0;
      else
      {
         if(s > 170)
         {
            double mult = sin(0.5f * sc*3.14159265358979323846) * 2 * zi(s, sc);
            result = lgamma(s);
            result -= s * log(2 * 3.1415926358979323846264);
            if(result > 709)
               return 1./0.;
            result = exp(result);
            if(1.7976931348623157081e+308 / fabs(mult) < result)
               return 1./0.;
            result *= mult;
         }
         else
         {
            result = sin(0.5f * sc*3.14159265358979323846)
               * 2 * pow(2 * 3.14159265358979323846264, -s) 
               * tgamma(s) 
               * zi(s, sc);
         }
      }
   }
   else
   {
      result =zip(s,sc);
   }
   return result;
}
double Zeta(double s){
	return zi(s,1-s);
}

伯努利数列1-129
阶乘数列1-170
包含进了代码里

你可能感兴趣的:(算法)