1 2 Previous Next 22 Replies Latest reply: Aug 20, 2007 4:08 PM by 843853

# All 4 digit combinations

Hello all I recently found a piece of code here I altered it slightly to serve my purpose.
I want it to list out all possible combinations of 4 numbers out of from 0 to 9. The code below will do this but the problem is it repeats the same combinations over and over. Can anyone alter the code to just print each 4 digit combinatin once.
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++) {

int 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);
}

}
}

bert.
• ###### 1. Re: All 4 digit combinations
``````  static void crap(){
for(int i = 0; i<7; i++){
for(int j = i+1; j<8; j++){
for(int k = j+1; k<9; k++){
for(int m = k+1; m<10; m++){
System.out.println(i+""+j+""+k+""+m);
}
}
}
}
}``````
thinking occasionally helps.
• ###### 2. Re: All 4 digit combinations
Thanks a lot for that sorry about the not thinking thing.
bert.
• ###### 3. Re: All 4 digit combinations
``````for (int i = 0; i <= 9999; i++) {
int a = i % 10;
int b = (i / 10) % 10;
int c = (i / 100) % 10;
int d = (i / 1000);

System.out.println(d + " " + c + " " + b + " " + a);
}``````
• ###### 4. Re: All 4 digit combinations
rkippen you are spot on well done and thanks a million.
bert.
• ###### 5. Re: All 4 digit combinations
``````for (int i = 0; i <= 9999; i++) {
int a = i % 10;
int b = (i / 10) % 10;
int c = (i / 100) % 10;
int d = (i / 1000);

System.out.println(d + " " + c + " " + b + " " +
+ a);
}``````
Horrendous... I love it :-D
• ###### 6. Re: All 4 digit combinations
``````final static NumberFormat number  = new DecimalFormat("0000");

public static void main(String[] args) throws Exception {
for (int i = 0; i <= 9; i++) {
int output=i;
for (int j = 0; j <= 9; j++) {
if (i == j) {
continue;
}
output=output*10+j;
for (int k = 0; k <= 9; k++) {
if (k == j|| k==i) {
continue;
}
output=output*10+k;
for (int l = 0; l <= 9; l++) {
if (k == l || l==j || l==i) {
continue;
}
output=output*10+l;

System.out.println(number.format(output));
output/=10;
}
output/=10;
}
output/=10;
}
output/=10;
}
}``````
• ###### 7. Re: All 4 digit combinations
Horrendous... I love it :-D
Yes, I agree :-)
• ###### 8. Re: All 4 digit combinations
Horrendous... I love it :-D
Yes, I agree :-)
I don't think that the algorithm does what is required. It just prints out all numbers from 0000 to 9999.

The following is a recursive way to solve the "taking 4 from 9" problem:
``````public class Permutation
{
private int maxDepth = 3;

public void take(char[] array,BitSet occupied,int depth,String print)
{
for(int i=0;i<array.length;++i)
{
if (occupied.get(i)==false)
{
if (depth<maxDepth)
{
occupied.flip(i);
take(array,occupied,depth+1,print+array);
occupied.flip(i);
}
else
{
System.out.println(print+array[i]);
}
}
}
}

/**
* @param args
*/
public static void main(String[] args)
{
char[] array = new char[]{'0','1','2','3','4','5','6','7','8','9'};

BitSet bs = new BitSet();

Permutation t = new Permutation();

t.take(array,bs,0,"");
}

}

It should give you the following:

0123
0124
0125
0126
0127
0128
0129
0132
0134
0135
...
9872
9873
9874
9875
9876 ``````
• ###### 9. Re: All 4 digit combinations
Forgot to mention, that by changing the private int maxDepth, you can print out other combinations.
• ###### 10. Re: All 4 digit combinations
how do i access then print this code??
• ###### 11. Re: All 4 digit combinations
how can u acess these codes what software????
• ###### 12. Re: All 4 digit combinations
Contact Sun about the pricing of the "codes". I think idiots get charged at a higher rate.
• ###### 13. Re: All 4 digit combinations
Heres my untested program with possible syntax errors, but at least a simple idea ;)
``````int a = 0;
int b = 0;
int c = 0;
int d = 0;
int count = 0;
for ( ; a<10; a++ )
for ( ; b<10; b++ )
for ( ; c<10; c++ )
for ( ; d<10; d++ )
{
if (a!=b && a!=c && a!=d && b!=c && b!=d && c!=d)
{
System.out.println( a+""+b+""+c+""+d );
count++;
}
}
System.out.println( count + " combinations" );``````
yey for brute force...
• ###### 14. Re: All 4 digit combinations
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.
1 2 Previous Next