Friends Tell me in theory, what are the practices to the approach to calculate the number of possible combinations from the elements of a one-dimensional array. And go through all possible sequences.
For example, there are elements of 2,45,16,34 – how to find out the possible combinations? (I do not ask the code, I ask the theory)
Answer 1, Authority 100%
permutations
Rearrangements is a combination of the initial array obtained by the permutation of the elements. The number of permutations AN = N! Algorithm for obtaining permutations by number (1..n!) Such:
Answer 2, Authority 19%
I do not know how it is done on PHP, but in theory you can apply the following algorithm:
Catch all the numbers from 0 (no element) to (2 ^ n) – 1 (all elements), where N is the length of the massive.
At every step of the search, we see all the bits of the number, and if the i-th bit is equal to one, the i-th element will be included in the combination.
For example, for three items:
- 0 – no elements
- 1 – (001) Only 1st element
- 2 – (010) Only 2nd
- 3 – (011) 2y and 1st
- 4 – (100) Only 3rd
- 5 – (101) 1y and 3rd
- 6 – (110) 1y and 2nd
- 7 – (111) 1y, 2nd, 3rd
Answer 3
var facts = [];
FUNCTION FACT (N) {
if (n == 0 || n == 1) Return 1;
if (Facts [N]) RETURN FACTS [N];
Facts [N] = N * FACT (N-1);
RETURN FACTS [N];
}
FUNCTION PERMUTIATION (INDEX, A) {
var n = a.length;
var i = index + 1;
var res = [];
for (var t = 1; t & lt; = n; t ++) {
VAR F = FACT (N-T);
var k = math.floor ((i + F-1) / F);
res.push (a.splice (K-1,1) [0]);
i - = (k - 1) * f;
}
if (a.length) res.push (A [0]);
RETURN RES;
}
FUNCTION LOG () {
var msg = array.prototype.slice.call (arguments) .join ("");
Document.getelementByid ("Log"). Value + = "\ N" + MSG;
Console.log (arguments);
}
VAR M = ["P", "O", "B", "A", "E", "C", "P", "R", "O", "C", "AND", " "];
For (var i = 0; i & lt; FACT (M.Length); I ++) {
log (i, permutation (i, m.slice (0)). join (""));
}