1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright © 2026 Mikhail Hogrefe
//
// Uses code adopted from the FLINT Library.
//
// Copyright © 2011 Fredrik Johansson
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
use crate::num::arithmetic::traits::{CheckedRisingFactorial, RisingFactorial, UnsignedAbs};
use crate::num::basic::signeds::PrimitiveSigned;
use crate::num::basic::unsigneds::PrimitiveUnsigned;
use crate::num::conversion::traits::SaturatingFrom;
// Computes the rising factorial by a checked product of consecutive factors. Partial products never
// exceed the final magnitude, since every remaining factor has magnitude at least 1, so `None`
// means exactly that the result is unrepresentable.
//
// This is the loop of rfac from fmpz/rfac.c, FLINT 3.6.0, with overflow reported rather than
// assumed away.
private_test_fn! {checked_rising_factorial_unsigned<T: PrimitiveUnsigned>(
x: T,
n: u64,
) -> Option<T> {
if n == 0 {
return Some(T::ONE);
}
if x == T::ZERO {
return Some(T::ZERO);
}
let mut f = x;
let mut factor = x;
for _ in 1..n {
factor = factor.checked_add(T::ONE)?;
f = f.checked_mul(factor)?;
}
Some(f)
}}
// The signed case must detect a factor sequence that reaches or crosses zero before multiplying:
// the result is then an exactly representable zero, but the partial products leading up to the zero
// factor may not be. This mirrors the negative-base analysis of fmpz_rfac_ui from fmpz/rfac.c,
// FLINT 3.6.0, where the span check picks between a zero and a negated positive rising factorial;
// here the remaining all-negative product runs directly in the signed type, whose checked
// operations reach even the most negative value when the result is representable.
private_test_fn! {checked_rising_factorial_signed<
U: PrimitiveUnsigned + SaturatingFrom<u64>,
S: PrimitiveSigned + UnsignedAbs<Output = U>,
>(
x: S,
n: u64,
) -> Option<S> {
if n == 0 {
return Some(S::ONE);
}
if x <= S::ZERO && x.unsigned_abs() <= U::saturating_from(n - 1) {
return Some(S::ZERO);
}
let mut f = x;
let mut factor = x;
for _ in 1..n {
factor = factor.checked_add(S::ONE)?;
f = f.checked_mul(factor)?;
}
Some(f)
}}
macro_rules! impl_rising_factorial {
($t:ident) => {
impl RisingFactorial for $t {
type Output = $t;
/// Computes the rising factorial of a number: the product of the `n` consecutive
/// numbers starting at `self`, or 1 when `n` is 0.
///
/// If the result is too large to be represented, the function panics. For a function
/// that returns `None` instead, try
/// [`checked_rising_factorial`](CheckedRisingFactorial::checked_rising_factorial).
///
/// $$
/// f(x, n) = x^{(n)} = x (x + 1) \cdots (x + n - 1).
/// $$
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `Self::WIDTH`: once its
/// factors reach 2, a nonzero product at least doubles per factor, so the loop runs
/// $O(n)$ times before overflowing or finishing.
///
/// # Panics
/// Panics if the result is not representable.
///
/// # Examples
/// See [here](super::rising_factorial#rising_factorial).
#[inline]
fn rising_factorial(self, n: u64) -> $t {
self.checked_rising_factorial(n).unwrap()
}
}
impl CheckedRisingFactorial for $t {
/// Computes the rising factorial of a number: the product of the `n` consecutive
/// numbers starting at `self`, or 1 when `n` is 0. Returns `None` if the result cannot
/// be represented.
///
/// $$
/// f(x, n) = \operatorname{Some}(x^{(n)}) = \operatorname{Some}(x (x + 1) \cdots
/// (x + n - 1)),
/// $$
/// if the product is representable.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `Self::WIDTH`: once its
/// factors reach 2, a nonzero product at least doubles per factor, so the loop runs
/// $O(n)$ times before overflowing or finishing.
///
/// # Examples
/// See [here](super::rising_factorial#checked_rising_factorial).
#[inline]
fn checked_rising_factorial(self, n: u64) -> Option<$t> {
checked_rising_factorial_unsigned(self, n)
}
}
};
}
apply_to_unsigneds!(impl_rising_factorial);
macro_rules! impl_rising_factorial_signed {
($u:ident, $s:ident) => {
impl RisingFactorial for $s {
type Output = $s;
/// Computes the rising factorial of a number: the product of the `n` consecutive
/// numbers starting at `self`, or 1 when `n` is 0.
///
/// If the result is too large to be represented, the function panics. For a function
/// that returns `None` instead, try
/// [`checked_rising_factorial`](CheckedRisingFactorial::checked_rising_factorial).
///
/// $$
/// f(x, n) = x^{(n)} = x (x + 1) \cdots (x + n - 1).
/// $$
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `Self::WIDTH`: once its
/// factors reach 2, a nonzero product at least doubles per factor, so the loop runs
/// $O(n)$ times before overflowing or finishing.
///
/// # Panics
/// Panics if the result is not representable.
///
/// # Examples
/// See [here](super::rising_factorial#rising_factorial).
#[inline]
fn rising_factorial(self, n: u64) -> $s {
self.checked_rising_factorial(n).unwrap()
}
}
impl CheckedRisingFactorial for $s {
/// Computes the rising factorial of a number: the product of the `n` consecutive
/// numbers starting at `self`, or 1 when `n` is 0. Returns `None` if the result cannot
/// be represented.
///
/// A factor sequence that reaches or crosses zero has a product of exactly zero, which
/// is always representable.
///
/// $$
/// f(x, n) = \operatorname{Some}(x^{(n)}) = \operatorname{Some}(x (x + 1) \cdots
/// (x + n - 1)),
/// $$
/// if the product is representable.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `Self::WIDTH`: once its
/// factors reach 2, a nonzero product at least doubles per factor, so the loop runs
/// $O(n)$ times before overflowing or finishing.
///
/// # Examples
/// See [here](super::rising_factorial#checked_rising_factorial).
#[inline]
fn checked_rising_factorial(self, n: u64) -> Option<$s> {
checked_rising_factorial_signed::<$u, $s>(self, n)
}
}
};
}
apply_to_unsigned_signed_pairs!(impl_rising_factorial_signed);