Combinatorics is a branch of mathematics dealing with counting, arrangement, and combination of objects. Let's start with some basic concepts and then move on to code examples in JavaScript.

1. Factorial:

The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

// Factorial function
function factorial(n) {
  if (n === 0 || n === 1) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}

// Example usage
console.log(factorial(5)); // Output: 120

2. Permutations:

A permutation of a set is an arrangement of its elements. The number of permutations of n distinct objects taken r at a time is given by $P(n, r) = \frac{n!}{(n-r)!}$

// Permutation function
function permutation(n, r) {
  return factorial(n) / factorial(n - r);
}

// Example usage
console.log(permutation(5, 2)); // Output: 20

3. Combinations / Binomial Coefficient:

A combination of a set is a selection of r items from the set, where order does not matter. The number of combinations of n distinct objects taken r at a time is given by $C(n, r) = \frac{n!}{r!(n-r)!}$

// Combination function
function combination(n, r) {
  return factorial(n) / (factorial(r) * factorial(n - r));
}

// Example usage
console.log(combination(5, 2)); // Output: 10

These examples cover some basic combinatorial concepts. Feel free to experiment with different values and understand how these functions work. Combining these functions can help you solve more complex combinatorial problems.


Previous

Data Structures and Algorithms

Next

Approaches to find Combinations