View Source FastMath.Combinatorics

FastMath.Combinatorics provides fast and efficient combinatorics functions for Elixir, including implementations for:

  • Factorials
  • Permutations
  • Combinations
  • Variations
  • Efficient generation of combinations from a list

The library is optimized for performance, using iterative methods, symmetry, and other techniques to minimize unnecessary computations.

Features

  • Factorials: Compute ( n! ) efficiently using permutations.
  • Permutations: Compute ( P(n, r) = n! / (n-r)! ) using an iterative approach.
  • Combinations: Compute ( C(n, r) = n! / (r!(n-r)!) ) directly without full factorials.
  • Variations: Compute ( V(n, r) = n^r ).
  • Generate Combinations: Efficiently generate all ( r )-element combinations from a list, leveraging symmetry for optimization.

Installation

Add fastmath_combinatorics to your list of dependencies in mix.exs:

def deps do
  [
    {:fastmath_combinatorics, "~> 0.1.0"}
  ]
end

Then run:

mix deps.get

Documentation

Documentation can be generated using ExDoc and is available at HexDocs.

To generate the docs locally, run:

mix docs

Once published, the documentation will be available at HexDocs.

Examples

Factorial

Calculate the factorial of a number:

iex> FastMath.Combinatorics.factorial(5)
120

Permutations

Calculate the number of permutations ( P(n, r) ):

iex> FastMath.Combinatorics.permutations(5, 3)
60

Combinations

Calculate the number of combinations ( C(n, r) ):

iex> FastMath.Combinatorics.combinations(5, 3)
10

Variations

Calculate the number of variations ( V(n, r) ):

iex> FastMath.Combinatorics.variations(2, 3)
8

Generate Combinations

Generate all ( r )-element combinations from a list:

iex> FastMath.Combinatorics.generate_combinations([1, 2, 3, 4], 3)
[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]

Contributing

Contributions are welcome! If you have ideas for improvements or additional functionality, feel free to submit a pull request or open an issue.

License

This project is licensed under the MIT License.