2

Hello I'm new to programming and registered to this forum :)

So I created a little program with nested for loops that prints out all combinations of five numbers which can have a value from 0 to 5. With nested for-loops this works fine. But isn't there a cleaner solution? I tried it with calling the for loop itself, but my brain doesn't get the solution.. :(

//my ugly solution
int store1, store2, store3, store4, store5;
        for (int count = 0; count <= 5; count++) {
            store1 = count;
            for (int count2 = 0; count2 <= 5; count2++) {
                store2 = count2;
                for (int count3 = 0; count3 <= 5; count3++) {
                    store3 = count3;
                    for (int count4 = 0; count4 <= 5; count4++) {
                        store4 = count4;
                        System.out
                                .println(store1 + " " + store2 + " " + store4);
                }
 //I'm trying around with something like this 
    void method1() {
        for (int count = 0; count <= 5; count++) {
                    list.get(0).value = count;
            count++;
            method2();
        }
    }
    void method2() {
        for (int count = 0; count <= 5; count++) {
                    list.get(1).value = count;
            count++;
            method1();
        }
    }
4
  • 6
    I think you are looking for recursion Commented Dec 13, 2012 at 17:31
  • Yeah, by the search today I stumbled it sometimes, but I didn't really understand how to implement it. Commented Dec 13, 2012 at 17:35
  • Why do you increment count INSIDE the loop when it is being incremented in BY the for loop? Commented Dec 13, 2012 at 17:39
  • Oops! It's a mistake..->Thank you! Commented Dec 13, 2012 at 18:00

5 Answers 5

3

Usually when people try to use recursion or functional, using a loop is simpler or faster. However, in this case recursion is the simpler option in combination with a loop.

public static void method(List<Integer> list, int n, int m) {
   if (n < 0) {
       process(list);
   } else {
      for(int i = 0; i < m; i++) {
         list.set(n, i);
         method(list, n-1, m);
      }
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I'm trying it out.
2

I know that you are trying combinations but this might help.

Permutation with repetitions

When you have n things to choose from ... you have n choices each time!

When choosing r of them, the permutations are:

n × n × ... (r times) = n^r

//when n and r are known statically

class Permutation
{
    public static void main(String[] args)
    {
        char[] values = {'a', 'b', 'c', 'd'};
        int n = values.length;
        int r = 2; 

        int i = 0, j = 0;
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                System.out.println(values[j] + " " + values[i]);
            }
        }
    }
}


//when n and r are known only dynamically

class Permutation
{
    public static void main(String[] args)
    {
        char[] values = {'a', 'b', 'c', 'd'};
        int n = values.length;
        int r = 2; 
        int i[] = new int[r];
        int rc = 0;
        for(int j=0; j<Math.pow(n,r); j++)
        {

            rc=0;
            while(rc<r)
            {
                System.out.print(values[i[rc]] + " ");
                rc++;
            }
            System.out.println();
            rc = 0;
            while(rc<r)
            {
                if(i[rc]<n-1)
                {
                    i[rc]++;
                    break;
                }
                else
                {
                    i[rc]=0;
                }
                rc++;
            }
        }
    }
}

2 Comments

Thank you :) For a question with poor grammar (I'm learning English), I get six instructive answers in 30 minutes. Just amazing. :)
for me - it is the solution!
0

Something like this?

// Print all sequences of len(list)+n numbers that start w/ the sequence in list
void method( list, n ) {
    if ( list.length == n )
        // print list
    else for ( int c=0; c<=5; c++ ) {
        // add c to end of list
        method( list, n );
        // remove c from end of list
    }
}

Initial call would be method( list, 5 ) where list is initially empty.

1 Comment

Thank you, I'm trying it out.
0

here another interative but less elegant version

while (store1 < 6) {
        store5++;
        if (store5 == 6) {
            store5 = 0;
            store4++;
        }
        if (store4 == 6) {
            store4 = 0;
            store3++;
        }
        if (store3 == 6) {
            store3 = 0;
            store2++;
        }
        if (store2 == 6) {
            store2 = 0;
            store1++;
        }
        System.out.println(store1 + " " + store2 + " " + store3 + " " + store4 + " " + store5 + " ");
    }

Comments

0

The simplest code I can think of would tackle the problem with an entirely different approach:

public class TestA {
    public static void main(String[] argv) {
        for (int i=0; i<(6 * 6 * 6 * 6 * 6); ++i) {
            String permutation = Integer.toString(i, 6);
            System.out.println("00000".substring(permutation.length()) + permutation);
        }
    }
}

From your text (not your code) I gather you have 5 places and 6 symbols, which suggests there are 6 to the 5th power combinations. So the code just counts through those numbers and translates the number to the output combination.

Since this can also be viewed as a number system with base 6, it makes use of Integer.toString which already has formatting code (except the leading zeros) for this. Leading zeros are added where missing.

Comments

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.