Trying to do combinatorics, factorial is really rather essential. Yes it can be calculated using `reduce`

. However given there is a `sum`

function in the library shouldn’t there be a `product`

function.

I believe `product`

is required far less often than `sum`

, that’s why we haven’t included it in the standard library.

Could you illustrate the use cases of `product`

function?

The really obvious example is factorial, which is an important function for all work on combinatorics. Anyone working on any data science application that uses combinatorics will have an implementation of factorial – preferably as a postfix exclamation mark operator.

I’ll see if I can hunt out others.

Obviously this is easy to implement with reduce of fold, it is just the hassle, especially given many languages have a product function in the standard library. It is about surprise that it isn’t there in Kotlin more than anything.

Functions like `product()`

could probably go into a `kotlinx.math`

module. There are a lot of functions that could also go into this module, e. g. `cartesianProduct()`

(I use this often) and extension functions for mathematical functions from `java.lang.Math`

[1] (this was requested a few times) along with functions like `cot()`

(which is not in `Math`

).

[1] Math extension functions and float-math functions for Kotlin · GitHub

I agree that the functions are too specific to be included to Stdlib. Kotlinx.math is better place

This works for me. I will have a look at the kotlinx workflow.

I’m trying to start competitive programming with Kotlin. Just in last two weeks I had to manually implement factorials, combinations and permutations multiple times, cause there is nothing for it in kotlin library.

There are several mathematics libraries. For now KMath is probably the most comperhansive (disclaimer: I am an author).

Combinatorics are not implemented, but you can create PR (or at least create an issue) for them (probably into `kmath-stat`

module). The factorial could not be done in stdlib because there are no universal ways to do it:

- The computation complexity soon becomes quite heave and you have to use something like Stirling’s approximation, which is not always a desirable result (especially in cases like binomial/Poisson coefficients).
- Factorial goes to an integer overflow fast. In order to work with those numbers properly, you need iether to go into BigDecimal or in logariphm-based computations. Both are possible, but specific way depends on the problem.

As always, discussion in `mathematics`

channel in Kotlin slack would be welcome.