Saturday, November 29, 2014

Generate all possible combination of numbers in java

The Idea is fairly Simple. Break any number chosen in combination of four numbers.
Concept of solution: If we are choosing any number from "0-9" i.e total 10 numbers. Then to get all combinations of 4 numbers total trial will be 10*10*10*10=10000 trials.

package BreakNumber;

/**
 *
 * @author Manjeet
 */
public class BreaKNumber {

    public static void main(String[] args) {
        int[] first = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] second = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] third = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] four = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

        //ALL Possible combination will be
        //first.length * second.length * third.length * four.length
        int len = first.length;// * second.length * third.length * four.length;
        String test = "1001";// Number to break
        int count = 0;
        len = 10;
        String s = "";
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                for (int k = 0; k < len; k++) {
                    for (int l = 0; l < len; l++) {
                        count++;
                        s=first[i] + "" + second[j] + "" + third[k] + "" + four[l];
                        if (s.trim().equals(test)) {
                            System.out.println("NO OF TRY: " + count + " | NUMBER IS: " + s);
                        }
                    }
                }
            }
        }
    }
}

No comments:

Post a Comment

Generate all possible combination of numbers in java

The Idea is fairly Simple. Break any number chosen in combination of four numbers. Concept of solution: If we are choosing any number from...