1 2 Previous Next 22 Replies Latest reply: Aug 20, 2007 4:08 PM by 843853 Go to original post RSS
      • 15. Re: All 4 digit combinations
        843853
        Urm...
        my program gives the following result:

        0123
        0124
        0125
        0126
        0127
        0128
        0129
        0132
        0134
        0135
        ...
        9872
        9873
        9874
        9875
        9876

        So it actually skips numbers that have repeating digits...
        I think we've lost track of the question, well... at least i have :)
        • 16. Re: All 4 digit combinations
          796440
          why not simply:
          System.out.println("0000");
          for (int i = 1000; i < 10000;
          System.out.println(i++));
          Does exactly what you want, print every possible
          combination of numbers forming a sequence of 4 long.
          I know this is a resurrected zombie, but the above won't give, for instance, 0123.

          Also, it's not clear whether the OP wanted "4 from 9" (like a lotto) or simply all 4-digit combinations. The OP sounded like the latter, I thought, but a couple of posted solutions tackled the former, so I'm not really sure.
          • 17. Re: All 4 digit combinations
            843853
            I think we two things...
            a) that this discussion is going nowhere
            b) that the initial "codes" (lol) are awful and useless, do'th anyone agree?
            public class Test3 {
            public static void main(String[] args)
            {
            int arr[] = {0,1, 2, 3, 4,5,6,7,8,9};
            int a[]={1};
            printPermutations(new int[0],arr);
            int i=0;
            }

            static void printPermutations(int soFar[], int someArray[])
            {
            int i = 0;
            if(someArray.length == 0)
            {
                 int j = 0;
            for(int ndx = 0; ndx < 4; ndx++)
            {
            System.out.print(soFar[ndx] );
            }
            System.out.println("****");
            return;
            }

            for(int ele = 0; ele < someArray.length; ele++)
            {             
            newSoFar[] = new int[soFar.length + 1];
            for(int ndx = 0; ndx < soFar.length; ndx++)
            {
            newSoFar[ndx] = soFar[ndx];
            newSoFar[newSoFar.length - 1] = someArray[ele];
            int subArray[] = new int[someArray.length - 1];
            for(int ndx = 0; ndx < someArray.length; ndx++)
            {
            if(ndx < ele) subArray[ndx] =someArray[ndx];
            if(ndx > ele) subArray[ndx - 1] =someArray[ndx];
            }
            printPermutations(newSoFar, subArray);
            }
            }
            }
            }
            • 18. Re: All 4 digit combinations
              jwenting
              why not simply:
              System.out.println("0000");
              for (int i = 1000; i < 10000;
              System.out.println(i++));
              Does exactly what you want, print every possible
              combination of numbers forming a sequence of 4
              long.


              I know this is a resurrected zombie, but the above
              won't give, for instance, 0123.
              ah yes, but it could be easily adjusted to give that as well of course.
              • 19. changing the subject
                843853
                what happens if the user gets to choose the number of digits?

                lets say what you are using as a four-nested loop will become an x-nested loop where the user gets to input x. How is this done without having to write every possible loop? Let say there could be 1<x<20 loops.

                I have heard that this can be solved recursively, is that true? how is that done?

                Thanks in advance!
                • 20. Re: changing the subject
                  843853
                  uj2n wrote:
                  I have heard that this can be solved recursively, is that true?
                  Yes.
                  how is that done?
                  Recursively.
                  • 21. The recursive solution
                    843853
                    Hope you understand it :
                    public static void printPermutations(){
                    
                              int[] per = new int[4];
                              printPermutations(per , 0);
                    
                         }
                    
                         private static  void printPermutations(int[] per , int index){
                    
                              //Base case          
                              if(index == per.length){
                                   print(per);
                                   return;
                              }
                    
                              for(int i=0; i<=9; i++){
                                   per[index] = i;
                                   //The recursive call
                                   printPermutations(per , index+1);
                              }
                         
                         }
                    
                         private static void print(int[] arr){
                              for(int k=0; k<arr.length; k++){
                                   System.out.print(arr[k]);
                              }
                              System.out.println();
                         }
                    
                    
                    
                         public static void main(String args[]){
                    
                              printPermutations();
                    
                         }
                    • 22. Re: The recursive solution
                      791266
                      koro91 wrote:
                      Hope you understand it :
                      Hi,

                      Nice to see that you want to help, but please, don't resurrect old threads. The question was asked about 4 years ago.

                      I'm locking this thread.

                      Kaj
                      1 2 Previous Next