Download the PHP package lishack/combinatorics-php without Composer
On this page you can find all versions of the php package lishack/combinatorics-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package combinatorics-php
PHP Combinatorics
A modern, type-safe combinatorics library for PHP 8.4+.
PHP Combinatorics provides efficient algorithms for counting, generating, ranking, and unranking combinatorial objects. It is designed for correctness, performance, and arbitrary-precision arithmetic using Brick\Math.
Unlike many combinatorics libraries, this project includes lazy generators, combination ranking and unranking algorithms, and exact calculations for extremely large numbers.
Features
- 🚀 Modern PHP 8.4+ API
- 🔢 Arbitrary-precision arithmetic via
Brick\Math - 📊 Counting algorithms
- Factorial
- Binomial coefficient
- Permutations
- Variations
- Combinations
- Variations with repetition
- Combinations with repetition
- ⚡ Lazy generators
- Combinations
- Combinations with repetition
- Permutations
- Variations
- Variations with repetition
- Power set
- Cartesian product
- 🎯 Combination ranking
- Lexicographic order
- Colexicographic order
- 🔄 Combination unranking
- 🧩 Supports custom objects
- ✅ Strict typing
- ✅ PHPUnit tested
- ✅ PHPStan Level 8
Requirements
- PHP 8.4 or later
- Brick\Math
- Webmozart Assert
Installation
Install the package via Composer:
Why PHP Combinatorics?
Many existing PHP combinatorics libraries focus solely on counting functions or rely on native integers, which overflow for larger values.
PHP Combinatorics is designed to provide a complete toolkit for combinatorial mathematics with a modern, type-safe API.
Highlights
- Exact calculations using arbitrary-precision integers.
- Memory-efficient lazy generators.
- Combination ranking and unranking.
- Modern PHP 8.4 API with strict typing.
- Thoroughly tested and statically analyzed.
Documentation
The Combinatorics class provides a simple, static API for all supported combinatorial operations.
The library is divided into three main categories:
- Counting – Calculate the number of possible arrangements without generating them.
- Generators – Lazily generate combinatorial objects one at a time.
- Ranking – Convert combinations to numeric ranks and reconstruct them from those ranks.
All counting methods return a Brick\Math\BigInteger, allowing exact calculations without integer overflow.
Counting
Counting methods calculate the number of possible results without generating them.
| Method | Description |
|---|---|
factorial() |
Calculates the factorial of a non-negative integer. |
binomial() |
Calculates the binomial coefficient C(n, k). |
permutationsCount() |
Calculates the number of permutations. |
variationsCount() |
Calculates the number of variations without repetition. |
variationsWithRepetitionCount() |
Calculates the number of variations with repetition. |
combinationsCount() |
Calculates the number of combinations without repetition. |
combinationsWithRepetitionCount() |
Calculates the number of combinations with repetition. |
Generators
Generator methods produce combinatorial objects lazily.
Instead of allocating every result in memory, values are generated only when requested during iteration.
| Method | Description |
|---|---|
combinations() |
Generates combinations without repetition. |
combinationsWithRepetition() |
Generates combinations with repetition. |
permutations() |
Generates all permutations. |
variations() |
Generates variations without repetition. |
variationsWithRepetition() |
Generates variations with repetition. |
powerSet() |
Generates the power set. |
cartesianProduct() |
Generates the Cartesian product of multiple sets. |
Ranking
Ranking methods assign unique numeric identifiers to combinations.
These methods are useful when combinations need to be stored, indexed, transferred, or reconstructed efficiently.
| Method | Description |
|---|---|
combinationRank() |
Calculates the rank of a combination. |
combinationUnrank() |
Restores a combination from its rank. |
Examples
Counting
Factorial
Output
Binomial Coefficient
Output
Permutations Count
Output
Variations Count
Output
Variations Count With Repetition
Output
Combinations Count
Output
Combinations Count With Repetition
Output
Generators
Combinations
Output
Combinations With Repetition
Output
Permutations
Output
Variations
Output
Variations With Repetition
Output
Power Set
Output
Cartesian Product
Output
Ranking
Combination Rank (Lexicographic)
Output
The combinations are ordered as follows:
| Rank | Combination |
|---|---|
| 0 | [A, B] |
| 1 | [A, C] |
| 2 | [A, D] |
| 3 | [B, C] |
| 4 | [B, D] |
| 5 | [C, D] |
Combination Rank (Colexicographic)
Output
The combinations are ordered as follows:
| Rank | Combination |
|---|---|
| 0 | [A, B] |
| 1 | [A, C] |
| 2 | [B, C] |
| 3 | [A, D] |
| 4 | [B, D] |
| 5 | [C, D] |
Combination Unrank
Output
Ranking Custom Objects
Objects can be ranked by providing a key selector.
Output
Why Lazy Generation?
All generators return results one at a time.
Instead of allocating every possible result in memory, values are produced only when requested by the iterator.
This makes the library suitable for working with very large combinatorial spaces where generating every result at once would be impractical.