If N is fixed,like N = 3, then it is easy, i can use nested loops of depth 3. e.g.
from i in Enumerable.Range(0, 2)
from j in Enumerable.Range(0, 2)
from k in Enumerable.Range(0, 2)
select new int[] { i, j, k };
what if N is a variable?
If N is fixed,like N = 3, then it is easy, i can use nested loops of depth 3. e.g.
from i in Enumerable.Range(0, 2)
from j in Enumerable.Range(0, 2)
from k in Enumerable.Range(0, 2)
select new int[] { i, j, k };
what if N is a variable?
What you need is some sort of array 'multiplyer'. Something like this:
private static IEnumerable<int[]> Multiply(IEnumerable<int[]> input,
IEnumerable<int> multiplyers)
{
foreach (var array in input)
{
foreach (var multiplyer in multiplyers)
{
yield return array.Concat(new int[] { multiplyer })
.ToArray();
}
}
}
You can use this method as follows to get the same result as your example above:
int n = 3;
var multiplyers = Enumerable.Range(0, 2);
IEnumerable<int[]> results =
from m in multiplyers select new int[] { m };
while (n-- > 1)
{
results = Multiply(results, multiplyers);
}
Now n
is variable.