6
I have a function that generates all possible combinations of numbers from 1 to 10. The combinations are 4 numbers, that is, each combination has 4 different numbers. Ex:
[1,2,3,4], [1,2,3,5], [1,2,3,6] ... [7,8,9,10];
I can specify the range of possible numbers, which is currently 1 to 10.
My problem is that I cannot specify the amount of numbers that will make the combination. Which currently is 4 to another number.
I already broke my head to try to leave the dynamic function and only pass the parameters(in case it would be the range of numbers and the quantity of numbers of a combination), so:
GerarCombinacoes(10, 4); //onde 10 seria o range de numeros de 1 a 10 e 4 a quantidade de numeros da combinacao
My function is like this at the moment:
function GerarCombinacoes() {
totalCombinacoes = 0;
numeroMaximo = 10; //range de 1 a 10
for (a = 1; a <= numeroMaximo ; a++) {
for (b = 2; b <= numeroMaximo ; b++) {
for (c = 3; c <= numeroMaximo ; c++) {
for (d = 4; d <= numeroMaximo ; d++) {
if ((a != d && a != c && a != b)) {
if ((b != d && b != c && b != a) && b > a) {
if ((c != d && c != b && c != a) && c > b) {
if ((d != c && d != b && d != a) && d > c) {
numeros = a + " - " + b + " - " + c + " - " + d;
totalCombinacoes++;
$("#Numeros").append("<p style='margin-left: 10px'>("+ totalCombinacoes+ ") " + numeros + "</p>");
}
}
}
}
}
}
}
}
alert(totalCombinacoes);
}
If I want to increase the number amount of the combination I need to put one more go and one more check to not repeat numbers, but so it gets complicated because there are some cases I would need to do more than 20 is all in hand.
Just so you understand. These ifs are combination rules, right? Is there any way to pass the combination rule? Because at the beginning of the post you say your script "generates all possible combinations of numbers from 1 to 10", but it’s not true, because the combination "1 - 1 - 1 - 1" is not in your rule. Pass us the combination rule and we try to help.
– Adriano Leal
They are different combinations, and with the 4 different numbers. Regardless of the order they appear, they cannot be repeated. ifs guarantee this.
– Joao Paulo
Okay and another rule would be to not consider the number zero, right?
– Adriano Leal
Exactly, in the first sentence of the question I mention these two pieces of information.
– Joao Paulo
It’s just that you said "you’re 1 to 10 at the moment" and from what I understand you want a generic function, but beauty. I’ll try to set up a function to help too.
– Adriano Leal
Sorry. The range will always be 1 until the number I set.
– Joao Paulo
I suggest that the staff give a reminder on combinatorics before "playing" directly into the code - I saw some solutions that, while working, are unnecessarily inefficient, even taking into account the complexity order of the problem. (i.e. the number of combinations is
n!/(m!*(n-m)!)
but saw order solutions ofn^m
,n^m * log(n^m)
and even2^n
...)– mgibsonbr
P.S. Not necessarily in order from best to worst...
– mgibsonbr