Variable Number of Nested For Loops

• 03-04-2012, 03:11 AM
atomant
Variable Number of Nested For Loops
I want to have some code that uses a different number of nested for loops, depending on input from the user. Is there an easy way to do this?

I was thinking an if statement before each for loop, but if numTimes, in the following code, is less than 3 then the println is unreachable. I haven't had any success trying anything else either.

Code:

```if (numTimes > 0) for (int i = 0; i < 3; i++) {             if (numTimes > 1) for (int j = 0; j < 3; j++) {                     if (numTimes > 2) for (int k = 0; k < 3; k++) {                             System.out.println(i+j+k);                     }       } }```
• 03-04-2012, 04:50 AM
pbrockway2
Re: Variable Number of Nested For Loops
How big will numTimes get?

For moderate values you are just printing the sum of the (decimal) digits of all the numbers from 0 to 3^numTimes in order which you can do in a single for loop.

For larger values (ie if 3^numTimes is bigger than a long) you could write a recursive method.
• 03-04-2012, 04:55 AM
Eranga
Re: Variable Number of Nested For Loops
Quote:

Originally Posted by pbrockway2
For larger values (ie if 3^numTimes is bigger than a long) you could write a recursive method.

Yeah, much better much safer.
• 03-04-2012, 05:49 AM
pbrockway2
Re: Variable Number of Nested For Loops
It strikes me that a long is ... long. If that value is exceeded who is going to take the time to actually read all those System.out.println()s? If the OP is actually doing something with them (like add them up or some such project Eulerish thing) then a little thought might save a lot of calculation.

Mostly posting because I noticed a typo in my previous: I meant that what is printed is the sum of the ternary digits of 1 -> 3^numTimes.
• 03-04-2012, 07:34 AM
atomant
Re: Variable Number of Nested For Loops
The code I posted wasn't what I'm trying to do, I was just posting a simple example to see if the for loop thing could be done.

What I'm trying to do is have all permutations listed. For example, if I had 3 nested for loops from 0 to 3 I would get the result:
000, 001, 002, 010, 011, 012, 020, 021, etc

But I would like to be able to input an int x instead of the static 3, and get the permutations for x characters. Like having x number of nested for loops.

Can I do this using a recursive method? I'm trying but it seems like it would be pretty challenging.
• 03-04-2012, 08:20 AM
JosAH
Re: Variable Number of Nested For Loops
Quote:

Originally Posted by atomant
The code I posted wasn't what I'm trying to do, I was just posting a simple example to see if the for loop thing could be done.

What I'm trying to do is have all permutations listed. For example, if I had 3 nested for loops from 0 to 3 I would get the result:
000, 001, 002, 010, 011, 012, 020, 021, etc

Those are combinations, not permutations; combinations can be enumerated by simpe counting (here in base 3). You don't need recursion for that and you don't need recursion for permutations either although that's the solution shown in text books.

kind regards,

Jos