PHP Iterators and Generators to generate combinations and permutations in an efficient way.
At first the library was created to only generate permutations and combinations.
In the end, I added other Iterators and Generators like:
- Fibonacci numbers,
- Perfect numbers,
- Prime numbers,
- Product of numbers,
- Rotation of an array,
- Cycling through an array,
- Permutations,
- Combinations,
I've always been fascinated by numbers and everything around them... in other words, mathematics.
The library has been written first for being used in PHPartition, then it has been extended here and there.
Its main use is for generating Permutations and Combinations without running out of memory, thanks to PHP Generators and and Iterators.
The difference with other combinatorics library is that you can use an extra parameter 'length', that allows you to compute Permutations and Combinations of a particular size. The other notable difference is that your input arrays may contains any type of object (integers, arrays, strings or objects), the library will still continue to work without any trouble.
- PHP >= 7.1.3,
Include this library in your project by doing:
composer require drupol/phpermutations
Let's say you want to find all the permutations of the list of number [1, 2, 3, 4, 5] having a length of 3:
// Create the object
$permutations = new \drupol\phpermutations\Generators\Permutations([1,2,3,4,5], 3);
// Use a foreach loop.
foreach ($permutations->generator() as $permutation) {// do stuff}
// Or get the whole array at once.
$permutations->toArray();Most of the components always has the same arguments except for very few of them.
As the documentation per component is not written yet, I advise you to check the tests to see how to use them.
In mathematics, a combination is a way of selecting items from a collection, such that (unlike permutations) the order of selection does not matter. -- Wikipedia
In one sentence: When the order doesn't matter, it is a Combination.
Let's say we have a group of fruits:
$list = ['Apple', 'Pear', 'Banana', 'Orange']
and we want to find the combinations of length: 3, the result will be:
['Apple', 'Pear', 'Banana']
['Apple', 'Pear', 'Orange']
['Apple', 'Banana', 'Orange']
['Pear', 'Banana', 'Orange']
In mathematics, the notion of permutation relates to the act of arranging all the members of a set into some sequence or order, or if the set is already ordered, rearranging (reordering) its elements, a process called permuting. These differ from combinations, which are selections of some members of a set where order is disregarded. -- Wikipedia
In one sentence: When the order does matter, it is a Permutation.