14a238c70SJohn Marino /* mpfr_const_catalan -- compute Catalan's constant.
24a238c70SJohn Marino
3*ab6d115fSJohn Marino Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino
234a238c70SJohn Marino #define MPFR_NEED_LONGLONG_H
244a238c70SJohn Marino #include "mpfr-impl.h"
254a238c70SJohn Marino
264a238c70SJohn Marino /* Declare the cache */
274a238c70SJohn Marino MPFR_DECL_INIT_CACHE(__gmpfr_cache_const_catalan, mpfr_const_catalan_internal);
284a238c70SJohn Marino
294a238c70SJohn Marino /* Set User Interface */
304a238c70SJohn Marino #undef mpfr_const_catalan
314a238c70SJohn Marino int
mpfr_const_catalan(mpfr_ptr x,mpfr_rnd_t rnd_mode)324a238c70SJohn Marino mpfr_const_catalan (mpfr_ptr x, mpfr_rnd_t rnd_mode) {
334a238c70SJohn Marino return mpfr_cache (x, __gmpfr_cache_const_catalan, rnd_mode);
344a238c70SJohn Marino }
354a238c70SJohn Marino
364a238c70SJohn Marino /* return T, Q such that T/Q = sum(k!^2/(2k)!/(2k+1)^2, k=n1..n2-1) */
374a238c70SJohn Marino static void
S(mpz_t T,mpz_t P,mpz_t Q,unsigned long n1,unsigned long n2)384a238c70SJohn Marino S (mpz_t T, mpz_t P, mpz_t Q, unsigned long n1, unsigned long n2)
394a238c70SJohn Marino {
404a238c70SJohn Marino if (n2 == n1 + 1)
414a238c70SJohn Marino {
424a238c70SJohn Marino if (n1 == 0)
434a238c70SJohn Marino {
444a238c70SJohn Marino mpz_set_ui (P, 1);
454a238c70SJohn Marino mpz_set_ui (Q, 1);
464a238c70SJohn Marino }
474a238c70SJohn Marino else
484a238c70SJohn Marino {
494a238c70SJohn Marino mpz_set_ui (P, 2 * n1 - 1);
504a238c70SJohn Marino mpz_mul_ui (P, P, n1);
514a238c70SJohn Marino mpz_ui_pow_ui (Q, 2 * n1 + 1, 2);
524a238c70SJohn Marino mpz_mul_2exp (Q, Q, 1);
534a238c70SJohn Marino }
544a238c70SJohn Marino mpz_set (T, P);
554a238c70SJohn Marino }
564a238c70SJohn Marino else
574a238c70SJohn Marino {
584a238c70SJohn Marino unsigned long m = (n1 + n2) / 2;
594a238c70SJohn Marino mpz_t T2, P2, Q2;
604a238c70SJohn Marino S (T, P, Q, n1, m);
614a238c70SJohn Marino mpz_init (T2);
624a238c70SJohn Marino mpz_init (P2);
634a238c70SJohn Marino mpz_init (Q2);
644a238c70SJohn Marino S (T2, P2, Q2, m, n2);
654a238c70SJohn Marino mpz_mul (T, T, Q2);
664a238c70SJohn Marino mpz_mul (T2, T2, P);
674a238c70SJohn Marino mpz_add (T, T, T2);
684a238c70SJohn Marino mpz_mul (P, P, P2);
694a238c70SJohn Marino mpz_mul (Q, Q, Q2);
704a238c70SJohn Marino mpz_clear (T2);
714a238c70SJohn Marino mpz_clear (P2);
724a238c70SJohn Marino mpz_clear (Q2);
734a238c70SJohn Marino }
744a238c70SJohn Marino }
754a238c70SJohn Marino
764a238c70SJohn Marino /* Don't need to save/restore exponent range: the cache does it.
774a238c70SJohn Marino Catalan's constant is G = sum((-1)^k/(2*k+1)^2, k=0..infinity).
784a238c70SJohn Marino We compute it using formula (31) of Victor Adamchik's page
794a238c70SJohn Marino "33 representations for Catalan's constant"
804a238c70SJohn Marino http://www-2.cs.cmu.edu/~adamchik/articles/catalan/catalan.htm
814a238c70SJohn Marino
824a238c70SJohn Marino G = Pi/8*log(2+sqrt(3)) + 3/8*sum(k!^2/(2k)!/(2k+1)^2,k=0..infinity)
834a238c70SJohn Marino */
844a238c70SJohn Marino int
mpfr_const_catalan_internal(mpfr_ptr g,mpfr_rnd_t rnd_mode)854a238c70SJohn Marino mpfr_const_catalan_internal (mpfr_ptr g, mpfr_rnd_t rnd_mode)
864a238c70SJohn Marino {
874a238c70SJohn Marino mpfr_t x, y, z;
884a238c70SJohn Marino mpz_t T, P, Q;
894a238c70SJohn Marino mpfr_prec_t pg, p;
904a238c70SJohn Marino int inex;
914a238c70SJohn Marino MPFR_ZIV_DECL (loop);
924a238c70SJohn Marino MPFR_GROUP_DECL (group);
934a238c70SJohn Marino
944a238c70SJohn Marino MPFR_LOG_FUNC (("rnd_mode=%d", rnd_mode),
954a238c70SJohn Marino ("g[%Pu]=%.*Rg inex=%d", mpfr_get_prec (g), mpfr_log_prec, g, inex));
964a238c70SJohn Marino
974a238c70SJohn Marino /* Here are the WC (max prec = 100.000.000)
984a238c70SJohn Marino Once we have found a chain of 11, we only look for bigger chain.
994a238c70SJohn Marino Found 3 '1' at 0
1004a238c70SJohn Marino Found 5 '1' at 9
1014a238c70SJohn Marino Found 6 '0' at 34
1024a238c70SJohn Marino Found 9 '1' at 176
1034a238c70SJohn Marino Found 11 '1' at 705
1044a238c70SJohn Marino Found 12 '0' at 913
1054a238c70SJohn Marino Found 14 '1' at 12762
1064a238c70SJohn Marino Found 15 '1' at 152561
1074a238c70SJohn Marino Found 16 '0' at 171725
1084a238c70SJohn Marino Found 18 '0' at 525355
1094a238c70SJohn Marino Found 20 '0' at 529245
1104a238c70SJohn Marino Found 21 '1' at 6390133
1114a238c70SJohn Marino Found 22 '0' at 7806417
1124a238c70SJohn Marino Found 25 '1' at 11936239
1134a238c70SJohn Marino Found 27 '1' at 51752950
1144a238c70SJohn Marino */
1154a238c70SJohn Marino pg = MPFR_PREC (g);
1164a238c70SJohn Marino p = pg + MPFR_INT_CEIL_LOG2 (pg) + 7;
1174a238c70SJohn Marino
1184a238c70SJohn Marino MPFR_GROUP_INIT_3 (group, p, x, y, z);
1194a238c70SJohn Marino mpz_init (T);
1204a238c70SJohn Marino mpz_init (P);
1214a238c70SJohn Marino mpz_init (Q);
1224a238c70SJohn Marino
1234a238c70SJohn Marino MPFR_ZIV_INIT (loop, p);
1244a238c70SJohn Marino for (;;) {
1254a238c70SJohn Marino mpfr_sqrt_ui (x, 3, MPFR_RNDU);
1264a238c70SJohn Marino mpfr_add_ui (x, x, 2, MPFR_RNDU);
1274a238c70SJohn Marino mpfr_log (x, x, MPFR_RNDU);
1284a238c70SJohn Marino mpfr_const_pi (y, MPFR_RNDU);
1294a238c70SJohn Marino mpfr_mul (x, x, y, MPFR_RNDN);
1304a238c70SJohn Marino S (T, P, Q, 0, (p - 1) / 2);
1314a238c70SJohn Marino mpz_mul_ui (T, T, 3);
1324a238c70SJohn Marino mpfr_set_z (y, T, MPFR_RNDU);
1334a238c70SJohn Marino mpfr_set_z (z, Q, MPFR_RNDD);
1344a238c70SJohn Marino mpfr_div (y, y, z, MPFR_RNDN);
1354a238c70SJohn Marino mpfr_add (x, x, y, MPFR_RNDN);
1364a238c70SJohn Marino mpfr_div_2ui (x, x, 3, MPFR_RNDN);
1374a238c70SJohn Marino
1384a238c70SJohn Marino if (MPFR_LIKELY (MPFR_CAN_ROUND (x, p - 5, pg, rnd_mode)))
1394a238c70SJohn Marino break;
1404a238c70SJohn Marino
1414a238c70SJohn Marino MPFR_ZIV_NEXT (loop, p);
1424a238c70SJohn Marino MPFR_GROUP_REPREC_3 (group, p, x, y, z);
1434a238c70SJohn Marino }
1444a238c70SJohn Marino MPFR_ZIV_FREE (loop);
1454a238c70SJohn Marino inex = mpfr_set (g, x, rnd_mode);
1464a238c70SJohn Marino
1474a238c70SJohn Marino MPFR_GROUP_CLEAR (group);
1484a238c70SJohn Marino mpz_clear (T);
1494a238c70SJohn Marino mpz_clear (P);
1504a238c70SJohn Marino mpz_clear (Q);
1514a238c70SJohn Marino
1524a238c70SJohn Marino return inex;
1534a238c70SJohn Marino }
154