# Formula command - factorial!

in formula command the factorial ! calculation not done .
is there is function to get factorial values and combination values

We do not have built-in functions for calculating factorial nor for combination values. But you can write your own functions for that. I prepared a small example:

Example:
F(4) = {=factorial(4)}
C(4, 2) = {=combination(4, 2)}

{factorial=(n)->reduce(seq(1,n), 1, (a, v)->a*v)}
{combination=(n, r)->factorial(n)/factorial(r)/factorial(n-r)}

Here how it works:

1. Lambda functions are used to create custom functions. You can read more about them here.
2. `seq(1,n)` - this is built-in function that creates an ordered list from 1 to `n`.
3. `reduce` - this is also built-in function that aggregates the elements of the list. It starts with `1` which is the second parameter, uses list that was generated by `seq` function and applies aggregation function which is a third parameter. In general it just multiplies all the elements of `seq`.

You can find all the available built-in functions here.

3 Likes

Small update, the factorial function was not working for `0`. Here an updated formula to fix that bug:

{factorial=(n)->reduce(seq(1,n), 1, (a, v)->a*v) if n > 0 else 1}