CombinatoricsSequences Method (Int32, Boolean) |
Namespace: Accord.Math
Suppose we would like to generate the same sequences shown in the TruthTable(Int32, Int32)example, however, without explicitly storing all possible combinations in an array. In order to iterate over all possible combinations efficiently, we can use:
int length = 3; // The number of variables; or number // of columns in the generated table. foreach (int[] row in Combinatorics.Sequences(length)) { // The following sequences will be generated in order: // // new int[] { 0, 0, 0 }, // new int[] { 0, 0, 1 }, // new int[] { 0, 1, 0 }, // new int[] { 0, 1, 1 }, // new int[] { 1, 0, 0 }, // new int[] { 1, 0, 1 }, // new int[] { 1, 1, 0 }, // new int[] { 1, 1, 1 }, }