0

I have the problem that I've got 6 for-loops but I want to remove the loops and replace them with a recursive/dynamic method.. sadly I dont have a clue how I can do this. Maybe one of you can help me.

for (int a = 1; a < 45; a++)
                {
                    for (int b = a + 1; b < 46; b++)
                    {
                        for (int c = b + 1; c < 47; c++)
                        {
                            for (int d = c + 1; d < 48; d++)
                            {
                                for (int e = d + 1; e < 49; e++)
                                {
                                    for (int f = e + 1; f < 50; f++)
                                    {                                           
                                        counter++;
                                        new_counter = zaehler.ToString("N0");
                                        Console.WriteLine(a + " " + b + " " + c + " " + d + " " + e + " " + f + " | -> " + new_counter);
                                        if (zaehler == 13983816)
                                        {
                                            Console.ForegroundColor = ConsoleColor.Green;
                                            Console.WriteLine($"{new_counter} combinations.");
                                            Console.ReadKey();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
10
  • 2
    This feels like a XY Problem - meta.stackexchange.com/questions/66377/what-is-the-xy-problem . Talk us through what the code should be doing. Commented Feb 7, 2019 at 10:09
  • What´s your actual problem? your code is pretty straight-forward to me. You could of course introduce some method, however I can´t see any reason to introduce some recursion here. Commented Feb 7, 2019 at 10:09
  • 3
    What do you mean by "dynamic" here? Also, what would recursion do for you other than maybe make things a bit slower and risk an overflow? Commented Feb 7, 2019 at 10:09
  • by "dynamic" here, do you mean "more flexible depth", so: not hard coded to 6? Commented Feb 7, 2019 at 10:15
  • @HimBromBeere I would like to have it more dynamic so when I want to change the values I dont have to change the code. In my opinion there would be a dynamic method better Commented Feb 7, 2019 at 10:16

1 Answer 1

3

The overall intent here is a little hard to grok, but here is a dynamically recursive version that seems to do something similar; I haven't fully checked it for equivalence:

static void RunLoop(int depth, int from, int to, Action<int, int[]> callback)
{
    int[] tokens = new int[depth];
    int counter = 0;
    RunLoop(tokens, 0, from, to, callback, ref counter);
}
private static void RunLoop(int[] tokens, int index, int from, int to,
    Action<int, int[]> callback, ref int counter)
{
    int nextIndex = index + 1;
    for(int i = from; i < to; i++)
    {
        tokens[index] = i;
        if (nextIndex == tokens.Length)
        {
            callback(counter, tokens);
            counter++; // post-increment when invoking the callback
        }
        else
        {
            counter++; // pre-increment when diving
            RunLoop(tokens, nextIndex, i + 1, to + 1, callback, ref counter);
        }
    }
}

with usage:

public static void Main()
{
    RunLoop(6, 1, 45, (i, arr) => Console.WriteLine(i + ": " + string.Join(", ", arr)));
}

Your "what to do when reaching the innermost depth" goes in callback; the first argument is the overall counter so far, and the second argument is the successive tokens that make up that value.

Sign up to request clarification or add additional context in comments.

2 Comments

Holy! I thank you so much! This had me left some sleepless nights.. holy shit you have paypal or anything where I can send you something??
@darby I have a buymeacoffee, but you really don't need to do that; I do this stuff for fun!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.