# Factorial

Factorial, denoted as \(n!\), represents the product of all positive integers less than or equal to a non-negative integer, \(n\). In simpler terms, given a non-negative integer, the factorial of that number is calculated by multiplying all the positive integers from \(1\) up to \(n\).

\begin{align*} n! &= n \times (n-1) \times (n-2) \times … \times 2 \times 1 \\ &= n \times (n-1)! \end{align*}

For example:

\[4! = 4 \times 3! = 4 \times 3 \times 2 \times 1 \ = 24\]

By convention, the factorial of \(0\) is equal to \(1\).

## Definition

In more formal terms, the factorial of a positive integer \(n\) can be expressed through the following recursive function:

\[ n! = \begin{cases} 1 & \text{ if } n = 0 \\[1em] n \times (n-1)! & \text{ if } n \in \mathbb{N}, n \gt 0 \end{cases} \]

The factorial can be also written more compactly as:

\[ n! = \prod_{k=1}^{n} k \]

That is, the product \(\prod\) of the values of \(k\) iterated starting from \(k=1\) to \(k=n\). The factorial formula, expressed compactly, becomes:

\[ n! = \begin{cases} 1 & \text{ if } n = 0 \\[1em] \displaystyle{\prod_{k=1}^{n} k} & \text{ if } n \in \mathbb{N}, n \gt 0 \end{cases} \]

The factorial formula is used to calculate the binomial coefficient, which represents the number of ways to select a specific number of items from a larger set.

## Factorial and rations

When working with ratios involving factorials, it is important to remember that these expressions often simplify significantly due to the cancellation of common factors. This situation is especially true when dealing with ratios of factorials that have a similar structure or contain common factors. By recognizing these patterns and simplifying the expression, we can arrive at a much more manageable form that is easier to work with and analyze.

For example, given the ratio of two factorials of the form:

\[\frac{n!}{(n-k)!}\]

were \(n\) and \(k\) are two integers and \(n \gt k\) we have:

\[\frac{n!}{(n-k)!} = n \times (n-1) \times (n-2) \times … \times (n -k +1) \]

## Example

Evaluate the ratio: \[\frac{10!}{5!}\]

In this example, \(n=10\) and \(n -k=5\). By the definition of factorial, we obtain:

\begin{align} \frac{10!}{5!} & = \frac{10 \cdot 9 \cdot 8 \cdot 7 \cdot 6 \cdot \overbrace{ 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1}^{5!}}{\underbrace{5 \cdot 4 \cdot 3 \cdot 2 \cdot 1}_{5!}}\\[1em] &= \frac{10 \cdot 9 \cdot 8 \cdot 7 \cdot 6 \cdot 5 !}{5 !} \\[0.6em] &= \frac{10 \cdot 9 \cdot 8 \cdot 7 \cdot 6 \cdot \cancel{5!}}{\cancel{5!}} \\[1em] &= 30240 \end{align}

The last term corresponds to \(n -k +1\), that is, \(10 -5 + 1 = 6\).

The solution is: \[\frac{10!}{5!} = 30240\]

## Factorial in combinatorics

In combinatorics, the factorial \(n!\) of a number \(n\) represents the number of sequences or permutations of \(n\) objects. For example, the number of possible permutations of 3 objects is \(3! = 3 \times (2)! = 6\).

In fact we have:

\begin{array}{rrrr} & o_1 & o_2 & o_3 \\ \hline & 1 & 2 & 3 \\ & 1 & 3 & 2 \\ & 2 & 1 & 3 \\ & 2 & 3 & 1 \\ & 3 & 1 & 2 \\ & 3 & 2 & 1 \\ \end{array}

## Recursive approach to code the factorial function

Factorial calculation is typically taken as an example when talking about recursive functions in programming due to its simplicity. A recursive function is a type of function that calls itself. Its defining characteristic is a recursive call that keeps being executed until the base case is reached. In the case of calculating a factorial, the base case is when \(n\) equals 0. Here is an example of a simple implementation of a factorial function in *Java*.

```
public class Factorial {
public static void main(String[] args) {
// Number to calculate the factorial of
int number = 5;
int factorial = calculateFactorial(number);
System.out.println("The factorial of " + number + " is: " + factorial);
}
public static int calculateFactorial(int n) {
if (n == 0) {
// The base case
return 1;
} else {
// Recursive call to calculate factorial
return n * calculateFactorial(n - 1);
}
}
}
```