1 /* mpz_bin_ui - compute n over k.
3 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
25 /* This is a poor implementation. Look at bin_uiui.c for improvement ideas.
26 In fact consider calling mpz_bin_uiui() when the arguments fit, leaving
27 the code here only for big n.
29 The identity bin(n,k) = (-1)^k * bin(-n+k-1,k) can be found in Knuth vol
30 1 section 1.2.6 part G. */
35 ASSERT (SIZ(r) > 0); \
36 MPN_DIVREM_OR_DIVEXACT_1 (PTR(r), PTR(r), (mp_size_t) SIZ(r), kacc); \
37 SIZ(r) -= (PTR(r)[SIZ(r)-1] == 0); \
41 mpz_bin_ui (mpz_ptr r, mpz_srcptr n, unsigned long int k)
51 /* bin(n,k) = (-1)^k * bin(-n+k-1,k), and set ni = -n+k-1 - k = -n-1 */
54 mpz_sub_ui (ni, ni, 1L);
55 negate = (k & 1); /* (-1)^k */
59 /* bin(n,k) == 0 if k>n
60 (no test for this under the n<0 case, since -n+k-1 >= k there) */
61 if (mpz_cmp_ui (n, k) < 0)
69 mpz_sub_ui (ni, n, k);
73 /* Now wanting bin(ni+k,k), with ni positive, and "negate" is the sign (0
74 for positive, 1 for negative). */
77 /* Rewrite bin(n,k) as bin(n,n-k) if that is smaller. In this case it's
78 whether ni+k-k < k meaning ni<k, and if so change to denominator ni+k-k
79 = ni, and new ni of ni+k-ni = k. */
80 if (mpz_cmp_ui (ni, k) < 0)
89 mpz_init_set_ui (nacc, 1L);
91 for (i = 1; i <= k; i++)
99 nacclow = PTR(nacc)[0];
100 for (c = 0; (((kacc | nacclow) & 1) == 0); c++)
105 mpz_div_2exp (nacc, nacc, c);
108 mpz_add_ui (ni, ni, 1L);
109 mpz_mul (nacc, nacc, ni);
110 umul_ppmm (k1, k0, kacc, i << GMP_NAIL_BITS);
111 k0 >>= GMP_NAIL_BITS;
114 /* Accumulator overflow. Perform bignum step. */
115 mpz_mul (r, r, nacc);
116 mpz_set_ui (nacc, 1L);
122 /* Save new products in accumulators to keep accumulating. */
127 mpz_mul (r, r, nacc);
129 SIZ(r) = (SIZ(r) ^ -negate) + negate;