- /* sdcrpi.cpp by K.Tsuru */
- // function ID 3508 DRADIX, constant pi since version 2.30
- /**************************************************************
- pi by Ramanujan's formula used by W.Gosper
- 2sqrt(2) (4n)!(1103+26390n)
- 1/pi = --------- sum_{n=0}^{\inf} ------------------
- 99^2 (4^n 99^n n!)^4
- sqrt(8) B(0)...B(n-1)
- =-------- sum_{n=0}^{\inf} --------------- A(n)
- 99^2 C(0)...C(n)
-
- Adding one term the presision rises by eight digits.
- See "sdcchpi.cpp" for detail.
- Binary Splitting method Nov.13,2016
- 28.4(sec) for 4190029 digits
- 69.5(sec) 8000029
- ***************************************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
-
- static const SLong D=1103L;
- static const SLong Ec=26390L; // To distinguish from E().
- static const SLong F=4L*99L;
- static const SLong G=99L*99L;
- static double upPerTerm = 7.98;
- #define BS_RJ_PI_SLONG 1 // SLong version is faster
- #if BS_RJ_PI_SLONG
-
- ////// SLong version /////// 2.36(sec) for 100000 digits
- class BSRamanujanPi : public BinarySplitting<SLong> {
- public:
- /* Constructer : 'L' is upToTerm, 'f' is precision*/
- BSRamanujanPi(long L, long f) : BinarySplitting<SLong>(L, f){}
-
- void setABC(long k, SLong& a, SLong& b, SLong& c) {
- // A(k)
- 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)
- if(k){ c = k*F; c *= c; c*=c; } // c = Lpow(k*F, 4);
- // if(k) c = Lpow(k*F, 4);
- else c = 1;
- }
-
- SDouble getValue() {
- putTogether();
- SDouble s = BinarySplitting<SLong>::getValue(true), pi = G*RecSqrt(8.0)*s;
- return pi;
- }
- };
- SDouble RamanujanPi() {
- SDouble C;
- long f = long(C.EffFig() + C.Hidden()+ 1u)*DFIGURES;
- long L = (long)((double)f/upPerTerm);
-
- BSRamanujanPi pi(L, f);
- return pi.getValue();
- }
- #else
- ////// SDouble version /////// 3.75(sec) for 100000 digits
- class BSRamanujanPi : public BinarySplitting<SDouble> {
- public:
- /* Constructer : 'L' is upToTerm, 'f' is precision*/
- BSRamanujanPi(long L, long f) : BinarySplitting<SDouble>(L, f){}
-
- void setABC(long k, SDouble& a, SDouble& b, SDouble& c) {
- // A(k)
- a = D + Ec * k;
-
- // B(k)= 8(k+1)(4k+3)(2k+1)(4k+1)
- SLong 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)
- if(k){ c = k*F; c *= c; c*=c; } // c = Lpow(k*F, 4);
- else c = 1;
- }
-
- SDouble getValue() {
- putTogether();
- SDouble s = BinarySplitting<SDouble>::getValue(true), pi = G*RecSqrt(8.0)*s;
- return pi;
- }
- };
- SDouble RamanujanPi() {
- SDouble C;
- long f = long( C.EffFig() + C.Hidden() ), L = long(upRate*double(f));
-
- BSRamanujanPi pi(L, f);
- return pi.getValue();
- }
-
- #endif // BS_RJ_PI_SLONG
sdcRjnpi.cpp : last modifiled at 2016/11/20 10:05:50(2,983 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).