14a238c70SJohn Marino /* bernoulli -- internal function to compute Bernoulli numbers.
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 #include "mpfr-impl.h"
244a238c70SJohn Marino
254a238c70SJohn Marino /* assuming b[0]...b[2(n-1)] are computed, computes and stores B[2n]*(2n+1)!
264a238c70SJohn Marino
274a238c70SJohn Marino t/(exp(t)-1) = sum(B[j]*t^j/j!, j=0..infinity)
284a238c70SJohn Marino thus t = (exp(t)-1) * sum(B[j]*t^j/j!, n=0..infinity).
294a238c70SJohn Marino Taking the coefficient of degree n+1 > 1, we get:
304a238c70SJohn Marino 0 = sum(1/(n+1-k)!*B[k]/k!, k=0..n)
314a238c70SJohn Marino which gives:
324a238c70SJohn Marino B[n] = -sum(binomial(n+1,k)*B[k], k=0..n-1)/(n+1).
334a238c70SJohn Marino
344a238c70SJohn Marino Let C[n] = B[n]*(n+1)!.
354a238c70SJohn Marino Then C[n] = -sum(binomial(n+1,k)*C[k]*n!/(k+1)!, k=0..n-1),
364a238c70SJohn Marino which proves that the C[n] are integers.
374a238c70SJohn Marino */
384a238c70SJohn Marino mpz_t*
mpfr_bernoulli_internal(mpz_t * b,unsigned long n)394a238c70SJohn Marino mpfr_bernoulli_internal (mpz_t *b, unsigned long n)
404a238c70SJohn Marino {
414a238c70SJohn Marino if (n == 0)
424a238c70SJohn Marino {
434a238c70SJohn Marino b = (mpz_t *) (*__gmp_allocate_func) (sizeof (mpz_t));
444a238c70SJohn Marino mpz_init_set_ui (b[0], 1);
454a238c70SJohn Marino }
464a238c70SJohn Marino else
474a238c70SJohn Marino {
484a238c70SJohn Marino mpz_t t;
494a238c70SJohn Marino unsigned long k;
504a238c70SJohn Marino
514a238c70SJohn Marino b = (mpz_t *) (*__gmp_reallocate_func)
524a238c70SJohn Marino (b, n * sizeof (mpz_t), (n + 1) * sizeof (mpz_t));
534a238c70SJohn Marino mpz_init (b[n]);
544a238c70SJohn Marino /* b[n] = -sum(binomial(2n+1,2k)*C[k]*(2n)!/(2k+1)!, k=0..n-1) */
554a238c70SJohn Marino mpz_init_set_ui (t, 2 * n + 1);
564a238c70SJohn Marino mpz_mul_ui (t, t, 2 * n - 1);
574a238c70SJohn Marino mpz_mul_ui (t, t, 2 * n);
584a238c70SJohn Marino mpz_mul_ui (t, t, n);
594a238c70SJohn Marino mpz_fdiv_q_ui (t, t, 3); /* exact: t=binomial(2*n+1,2*k)*(2*n)!/(2*k+1)!
604a238c70SJohn Marino for k=n-1 */
614a238c70SJohn Marino mpz_mul (b[n], t, b[n-1]);
624a238c70SJohn Marino for (k = n - 1; k-- > 0;)
634a238c70SJohn Marino {
644a238c70SJohn Marino mpz_mul_ui (t, t, 2 * k + 1);
654a238c70SJohn Marino mpz_mul_ui (t, t, 2 * k + 2);
664a238c70SJohn Marino mpz_mul_ui (t, t, 2 * k + 2);
674a238c70SJohn Marino mpz_mul_ui (t, t, 2 * k + 3);
684a238c70SJohn Marino mpz_fdiv_q_ui (t, t, 2 * (n - k) + 1);
694a238c70SJohn Marino mpz_fdiv_q_ui (t, t, 2 * (n - k));
704a238c70SJohn Marino mpz_addmul (b[n], t, b[k]);
714a238c70SJohn Marino }
724a238c70SJohn Marino /* take into account C[1] */
734a238c70SJohn Marino mpz_mul_ui (t, t, 2 * n + 1);
744a238c70SJohn Marino mpz_fdiv_q_2exp (t, t, 1);
754a238c70SJohn Marino mpz_sub (b[n], b[n], t);
764a238c70SJohn Marino mpz_neg (b[n], b[n]);
774a238c70SJohn Marino mpz_clear (t);
784a238c70SJohn Marino }
794a238c70SJohn Marino return b;
804a238c70SJohn Marino }
81