1 /* mpz_mfac_uiui(RESULT, N, M) -- Set RESULT to N!^(M) = N(N-M)(N-2M)...
3 Contributed to the GNU project by Marco Bodrato.
5 Copyright 2012, 2013 Free Software Foundation, Inc.
7 This file is part of the GNU MP Library.
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free Software
19 Foundation; either version 2 of the License, or (at your option) any
22 or both in parallel, as here.
24 The GNU MP Library is distributed in the hope that it will be useful, but
25 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 You should have received copies of the GNU General Public License and the
30 GNU Lesser General Public License along with the GNU MP Library. If not,
31 see https://www.gnu.org/licenses/. */
36 /*************************************************************/
37 /* Section macros: common macros, for swing/fac/bin (&sieve) */
38 /*************************************************************/
40 #define FACTOR_LIST_STORE(P, PR, MAX_PR, VEC, I) \
42 if ((PR) > (MAX_PR)) { \
43 (VEC)[(I)++] = (PR); \
49 /*********************************************************/
50 /* Section oder factorials: */
51 /*********************************************************/
53 /* mpz_mfac_uiui (x, n, m) computes x = n!^(m) = n*(n-m)*(n-2m)*... */
56 mpz_mfac_uiui (mpz_ptr x
, unsigned long n
, unsigned long m
)
58 ASSERT (n
<= GMP_NUMB_MAX
);
61 if ((n
< 3) | (n
- 3 < m
- 1)) { /* (n < 3 || n - 1 <= m || m == 0) */
62 PTR (x
)[0] = n
+ (n
== 0);
64 } else { /* m < n - 1 < GMP_NUMB_MAX */
69 g
= mpn_gcd_1 (&sn
, 1, m
);
70 if (g
> 1) { n
/=g
; m
/=g
; }
72 if (m
<= 2) { /* fac or 2fac */
80 mpz_2fac_ui (x
, n
<< 1);
95 } else { /* m >= 3, gcd(n,m) = 1 */
97 mp_limb_t prod
, max_prod
, j
;
105 max_prod
= GMP_NUMB_MAX
/ n
;
108 factors
= MPZ_NEWALLOC (x
, sn
/ log_n_max (n
) + 2);
111 factors
= TMP_ALLOC_LIMBS (sn
/ log_n_max (n
) + 2);
114 for (; n
> m
; n
-= m
)
115 FACTOR_LIST_STORE (n
, prod
, max_prod
, factors
, j
);
122 mpz_prodlimbs (t
, factors
, j
);
124 mpz_prodlimbs (x
, factors
, j
);
134 mpz_ui_pow_ui (p
, g
, sn
); /* g^sn */