Factorial in #rust , the #functional paradigm pays off well.
Factorial in #rust , the #functional paradigm pays off well.
@atamakahere Surely that would need to limit itself to u8 as the input and u128 as the output? And even then you'd need a hard limit enforced on the input value because 255! is equal to more than 3.35E104, whereas u128::MAX is less than 3.5E38.
In fact, the biggest number which fits is 34! equal to roughly 2.95E38.
Factorials grow up so fast.
@Corax42 @zethtren @bobulous that’s the neat part, it works for zero as well,
The `.product()` [1] is implemented using `fold` and starts from 1, so if it doesn’t have anything to accumulate, it returns 1, which is correct for 0 and 1 as input.
Also `RangeInclusive` doesn’t complain if the range is `(1..=0)`. because it is an fused iterator [2], it will return `None` once and `.product()` will do the job