combinations and iterations
in
Programming Questions
•
4 months ago
hi,
i am trying to write nested for loops to iterate through combinatorial number sequences, nCr. so far, i have gotten 4C2 to work:
1, 2
1, 3
1, 4
2, 3
2, 4
however, i am struggling with 4C3.
the loop iterates through:
1, 2, 3
1, 2, 4
1, 3, 4
and then stops and (l) instead of going to:
2, 3, 4
also my control loop doesn't decrease even though i have (i --) as the last condition.
this seems like a terrible way to do this; is there another type of data structure to do this. i would like to have these loops automated to work with whatever combinatorial number sequence it is given. however, i realized that the number of nests has to be equal to n, so that seems it would be difficult to add nests within a loop through iteration.
here's my code so far:
int n = 4;
int digit = 3;
int counter2 = 2;
int counter3 = 3;
void setup()
{
// combTwo();
println(combN(4, 3));
}
// my output for the following loop works except for (l). the sequence i get is (jkl) 123, 124, 134, 23.... it never goes back to l, which should be 4. instead j remains iterating between the values of 1 and 2.
void draw()
{
// for(int i = combN(n, digit); i > 0; i --)
// { println("i" + ", " + i);
for(int j = 1; j <= n - 2; j ++)
{ println("j" + ", " + j);
for(int k = counter2; k <= n - 1; k ++)
{ println("k" + ", " + k);
for(int l = counter3; l < n + 1; l ++)
{
println("l" + ", " + l);
// println(i + ", " + j + ", " + k + ", " + l + ", " + counter3);
}
counter3 ++;
}
counter2 ++;
}
// }
}
int factN(int n)
{
if (n <= 1) return 1;
else return n * factN(n - 1);
}
int combN(int n, int digit)
{
if (digit == n) return 1;
else return factN(n)/(factN(digit) * factN(n - digit));
}
void combTwo()
{
for(int i = combN(n, digit); i > 0; i --)
{
for(int j = 1; j <= n - 1; j ++)
{
for(int k = counter2; k < n + 1; k ++)
{
println(j + ", " + k);
}
counter2 ++;
}
}
}
thanks in advance.
cheers,
destro
i am trying to write nested for loops to iterate through combinatorial number sequences, nCr. so far, i have gotten 4C2 to work:
1, 2
1, 3
1, 4
2, 3
2, 4
however, i am struggling with 4C3.
the loop iterates through:
1, 2, 3
1, 2, 4
1, 3, 4
and then stops and (l) instead of going to:
2, 3, 4
also my control loop doesn't decrease even though i have (i --) as the last condition.
this seems like a terrible way to do this; is there another type of data structure to do this. i would like to have these loops automated to work with whatever combinatorial number sequence it is given. however, i realized that the number of nests has to be equal to n, so that seems it would be difficult to add nests within a loop through iteration.
here's my code so far:
int n = 4;
int digit = 3;
int counter2 = 2;
int counter3 = 3;
void setup()
{
// combTwo();
println(combN(4, 3));
}
// my output for the following loop works except for (l). the sequence i get is (jkl) 123, 124, 134, 23.... it never goes back to l, which should be 4. instead j remains iterating between the values of 1 and 2.
void draw()
{
// for(int i = combN(n, digit); i > 0; i --)
// { println("i" + ", " + i);
for(int j = 1; j <= n - 2; j ++)
{ println("j" + ", " + j);
for(int k = counter2; k <= n - 1; k ++)
{ println("k" + ", " + k);
for(int l = counter3; l < n + 1; l ++)
{
println("l" + ", " + l);
// println(i + ", " + j + ", " + k + ", " + l + ", " + counter3);
}
counter3 ++;
}
counter2 ++;
}
// }
}
int factN(int n)
{
if (n <= 1) return 1;
else return n * factN(n - 1);
}
int combN(int n, int digit)
{
if (digit == n) return 1;
else return factN(n)/(factN(digit) * factN(n - digit));
}
void combTwo()
{
for(int i = combN(n, digit); i > 0; i --)
{
for(int j = 1; j <= n - 1; j ++)
{
for(int k = counter2; k < n + 1; k ++)
{
println(j + ", " + k);
}
counter2 ++;
}
}
}
thanks in advance.
cheers,
destro
1