- /* sdcrpi2.cpp by K.Tsuru */
- // function ID 3512 DRADIX, constant pi since version 2.30
- /**************************************************************
- pi by Ramanujan's second formula
- (-1)^n (4n)!(1123+21460n)
- 3528/pi = sum_{n=0}^{\inf} ---------------------------
- 882^(2n)(4^n n!)^4
- B(0)...B(n-1)
- = sum_{n=0}^{\inf} --------------- A(n) = s
- C(0)...C(n)
-
- pi = 3528*s
- See below for A(n),B(k),C(k)
- Adding one term the presision rises by eight digits.
- See "sdcchpi.cpp" for detail.
-
- Binary Splitting method Nov.14,2016
- 49.1(sec) for 4000029 digits
- 91.8(sec) 8000029
- ***************************************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
-
- static const SLong D=1123L;
- static const SLong Ec=21460L; // To distinguish from E().
- static const SLong F=SLong(882L*882L)*SLong(256L);//882^2*4^4
- static const SLong G=4L*882L; // =3528
- static double upPerTerm = 5.891;
-
- #define BS_RJ_PI2_SLONG 1 // SLong version is faster
- #if BS_RJ_PI2_SLONG
-
- ////// SLong version /////// 0.66(sec) for 100000 digits
- class BSRamanujanPi2 : public BinarySplitting<SLong> {
- public:
- /* Constructer : 'L' is upToTerm, 'f' is precision*/
- BSRamanujanPi2(long L, long f) : BinarySplitting<SLong>(L, f){}
-
- void setABC(long k, SLong& a, SLong& b, SLong& c) {
- // A(k)=(-1)^k*(D+Ek)
- if(k&1) a = -D - Ec * k;
- else a = D + Ec * k;
- // B(k)= 8(k+1)(4k+3)(2k+1)(4k+1)
- if(k+1L < LONG_MAX/8L){
- long d =8*(k+1L), p = 4L*k+3L, q = 2L*k+1L, r = p-2L;
- b= d; b *= p; b *= q; b *= r;
- }else{
- SLong d =k+1L, p = 4L*k+3L, q = 2L*k+1L, r = p-2L;
- b= 8; b *= d; b *= p; b *= q; b *= r;
- }
- // C(k)=F*k^4
- if(k) c = F*Lpow(k, 4);
- else c = 1L;
- }
-
- SDouble getValue() {
- putTogether();
- SDouble s = BinarySplitting<SLong>::getValue(true), pi = G*s;
- return pi;
- }
- };
- SDouble RamanujanPi2() {
- SDouble C;
- long f = long(C.EffFig() + C.Hidden()+ 1u)*DFIGURES;
- long L = (long)((double)f/upPerTerm);
-
- BSRamanujanPi2 pi(L, f);
- return pi.getValue();
- }
- #else
- ////// SDouble version /////// 1.11(sec) for 100000 digits
- class BSRamanujanPi2 : public BinarySplitting<SDouble> {
- public:
- /* Constructer : 'L' is upToTerm, 'f' is precision*/
- BSRamanujanPi2(long L, long f) : BinarySplitting<SDouble>(L, f){}
-
- void setABC(long k, SDouble& a, SDouble& b, SDouble& c) {
- // A(k)=(-1)^k*(D+Ek)
- if(k&1) a = -D - Ec * k;
- else a = D + Ec * k;
- // B(k)= 8(k+1)(4k+3)(2k+1)(4k+1)
- long d =8L*(k+1L), p = 4L*k+3L, q = 2L*k+1L, r = p-2L;
- b = d; b *= p; b *= q; b *= r;
- // C(k)=F*k^4
- if(k) c = F*Lpow(k, 4);
- else c = 1L;
- }
-
- SDouble getValue() {
- putTogether();
- SDouble s = BinarySplitting<SDouble>::getValue(true), pi = G*s;
- return pi;
- }
- };
- SDouble RamanujanPi2() {
- SDouble C;
- long f = long( C.EffFig() + C.Hidden() ), L = long(upRate*double(f));
- BSRamanujanPi2 pi(L, f);
- return pi.getValue();
- }
-
- #endif // BS_RJ_PI_SLONG
sdcRjnpi2.cpp : last modifiled at 2017/08/25 15:35:46(3,051 bytes)
created at 2017/10/07 10:21:15
The creation time of this html file is 2017/10/07 10:30:03 (Sat Oct 07 10:30:03 2017).