I have a function that generates all possible combinations of numbers from 1 to 10. The combinations are 4 numbers, ie 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 can not specify the number of numbers that will match. Which is currently 4 for another number.
I have already broken my head to try to leave the dynamic function and just pass the parameters (in this case would be the range of numbers and the number of numbers of a combination), like this:
GerarCombinacoes(10, 4); //onde 10 seria o range de numeros de 1 a 10 e 4 a quantidade de numeros da combinacao
My role is now:
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 of numbers in the combination I need to add another for and one more check to not repeat numbers, but it gets tricky because there are some cases that I would need to do more than 20 for everything in the hand. p>