2

I am working in an Authomata and need three numbers that suumed equal n For example, if n = 2 the numbers I need are:

200
020
002
110
101
011

It doesn´t matter if the combination are repeated.

If n = 3 I need:

300
030
003
210
201
021
120
012
102
111

So I read thar this was similar to the partition in the theory of numbers but I can get the particular case where only 3 numbers give me the target value (n). (The code is from a example I get here)

package automata2;

import java.util.ArrayList;

/**
 *
 * @author jaime
 */
public class Automata2 {


    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        int N = 14;
        partition(N);
    }

    public static void partition(int n) {
        partition(n, n, "");
    }
    public static void partition(int n, int max, String prefix) {
        if (n == 0) {
            System.out.println(prefix);
            return;
        }

        for (int i = Math.min(max, n); i >= 1; i--) {
            partition(n-i, i, prefix + " " + i);
        }
    }
}

But I only need al the combinations with three digits, not all since 14 until 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Cœur
  • 37,241
  • 25
  • 195
  • 267
jaime_briones
  • 23
  • 1
  • 4
  • what's wrong with brute force here? – Leo Aug 03 '14 at 18:44
  • I am not pretty sure on how to do it, I tried a switch structure but the conditions where a pretty hard, I am new in programming, sorry if this is a easy question. – jaime_briones Aug 03 '14 at 18:49
  • looks like you need 2 nested for loops to generate all options, and for each option, you can test if the node satisfies your condition or not. It's called brute force because you generate all possible solutions just to find those few ones that you actually want. – Leo Aug 03 '14 at 18:52
  • could you show me please the algorithm for this? – jaime_briones Aug 03 '14 at 18:54
  • does it need to be recursive? – Leo Aug 03 '14 at 18:58
  • no it dosen´t, is there a way where you can use three variables iny type, because if n = 14, 14 is not a result I need, but 14 0 0 it is a result I need – jaime_briones Aug 03 '14 at 19:03
  • sorry, I mean't 3 nested loops. – Leo Aug 03 '14 at 19:06
  • You *really* could just google "java partition implementation"... – djechlin Aug 03 '14 at 19:15
  • 1
    @Leo actually, two loops are enough. The third number is better calculated as `N-n1-n2`. Additional bonus: much more efficient. – Henry Aug 03 '14 at 19:27
  • Maybe you would like to know what I am working on, here is it http://stackoverflow.com/questions/25109050/how-to-recursive-the-authomata-strange-planet-exercise I need some help also with recursive – jaime_briones Aug 03 '14 at 21:01

4 Answers4

0

One solution is to check number of numbers is three and then print it

    public static void main(String[] args) {
        int N = 14;
        partition(N);
    }

    public static void partition(int n) {
        partition(n, n, "");
    }
    public static void partition(int n, int max, String prefix) {
        // added condition here
        if (n == 0 && prefix.trim().split(" ").length == 3) {
            System.out.println(prefix);
            return;
        }

        for (int i = Math.min(max, n); i >= 1; i--) {
            partition(n-i, i, prefix + " " + i);
        }
    }

Output :

 12 1 1
 11 2 1
 10 3 1
 10 2 2
 9 4 1
 9 3 2
 8 5 1
 8 4 2
 8 3 3
 7 6 1
 7 5 2
 7 4 3
 6 6 2
 6 5 3
 6 4 4
 5 5 4

Using nested loops - only for 3 number solution

    int N = 14;
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= N; j++) {
            for (int k = 0; k <= N; k++) {
                if (i + j + k == N) {
                    System.out.println(i + " " + j + " " + k);              
                }           
            }       
        }   
    }

Output :

0 0 14
0 1 13
0 2 12
0 3 11
0 4 10
0 5 9
0 6 8
0 7 7
0 8 6
0 9 5
0 10 4
0 11 3
0 12 2
0 13 1
0 14 0
1 0 13
1 1 12
1 2 11
1 3 10
1 4 9
1 5 8
1 6 7
1 7 6
1 8 5
1 9 4
1 10 3
1 11 2
1 12 1
1 13 0
2 0 12
2 1 11
2 2 10
2 3 9
2 4 8
2 5 7
2 6 6
2 7 5
2 8 4
2 9 3
2 10 2
2 11 1
2 12 0
3 0 11
3 1 10
3 2 9
3 3 8
3 4 7
3 5 6
3 6 5
3 7 4
3 8 3
3 9 2
3 10 1
3 11 0
4 0 10
4 1 9
4 2 8
4 3 7
4 4 6
4 5 5
4 6 4
4 7 3
4 8 2
4 9 1
4 10 0
5 0 9
5 1 8
5 2 7
5 3 6
5 4 5
5 5 4
5 6 3
5 7 2
5 8 1
5 9 0
6 0 8
6 1 7
6 2 6
6 3 5
6 4 4
6 5 3
6 6 2
6 7 1
6 8 0
7 0 7
7 1 6
7 2 5
7 3 4
7 4 3
7 5 2
7 6 1
7 7 0
8 0 6
8 1 5
8 2 4
8 3 3
8 4 2
8 5 1
8 6 0
9 0 5
9 1 4
9 2 3
9 3 2
9 4 1
9 5 0
10 0 4
10 1 3
10 2 2
10 3 1
10 4 0
11 0 3
11 1 2
11 2 1
11 3 0
12 0 2
12 1 1
12 2 0
13 0 1
13 1 0
14 0 0
sujithvm
  • 2,351
  • 3
  • 15
  • 16
0

The algorithm is like this to generate the numbers. This code will generate all nos.

public class main1 {
    public static void main(String args[]) {

        int N = 5, n1, n2, n3;

        for (n1 = 0; n1 <= N; n1++) {
            for (n2 = 0; n2 <= N; n2++) {
                for (n3 = 0; n3 <= N; n3++) {
                    if ( (n1+n2+n3)==N ) {
                        System.out.println(n1 + " " + n2 + " " + n3);
                    }
                }
            }
        }
    }
}

what you can do is instead of System.out.println(n1+" "+n2+" "+n3), you can store the numbers in an array list then arrange the list.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
Mohammed Ali
  • 2,758
  • 5
  • 23
  • 41
0

One by one checking with skipping unnecessary partitions.

public static void main(String[] args) {
    int n =14;
    ArrayList<String> temp = partition(n);
    for (int z =0; z<temp.size(); z++){
        System.out.println(temp.get(z));
    }

}
public static ArrayList<String> partition(int n){
    ArrayList<String> out = new ArrayList<String>();
    int a=0, b=0, c=0;
    for (c=n; c>=0; c--){
        for(int j =a; j<=n;j++){
            if((c+j)>n){
                j=n+1;
                continue;
            }
            for (int i=b;i<=n; i++){
                int p=c+j+i;
                if (p>n) i=n+1;
                if(p==n){
                    String s = Integer.toString(c)+" "+ Integer.toString(j)+" "+ Integer.toString(i); 
                    out.add(s);
                }
            }

        }

    }
    return out;
}

Out put:

14 0 0
13 0 1
13 1 0
12 0 2
12 1 1
12 2 0
11 0 3
11 1 2
11 2 1
11 3 0
10 0 4
10 1 3
10 2 2
10 3 1
10 4 0
9 0 5
9 1 4
9 2 3
9 3 2
9 4 1
9 5 0
8 0 6
8 1 5
8 2 4
8 3 3
8 4 2
8 5 1
8 6 0
7 0 7
7 1 6
7 2 5
7 3 4
7 4 3
7 5 2
7 6 1
7 7 0
6 0 8
6 1 7
6 2 6
6 3 5
6 4 4
6 5 3
6 6 2
6 7 1
6 8 0
5 0 9
5 1 8
5 2 7
5 3 6
5 4 5
5 5 4
5 6 3
5 7 2
5 8 1
5 9 0
4 0 10
4 1 9
4 2 8
4 3 7
4 4 6
4 5 5
4 6 4
4 7 3
4 8 2
4 9 1
4 10 0
3 0 11
3 1 10
3 2 9
3 3 8
3 4 7
3 5 6
3 6 5
3 7 4
3 8 3
3 9 2
3 10 1
3 11 0
2 0 12
2 1 11
2 2 10
2 3 9
2 4 8
2 5 7
2 6 6
2 7 5
2 8 4
2 9 3
2 10 2
2 11 1
2 12 0
1 0 13
1 1 12
1 2 11
1 3 10
1 4 9
1 5 8
1 6 7
1 7 6
1 8 5
1 9 4
1 10 3
1 11 2
1 12 1
1 13 0
0 0 14
0 1 13
0 2 12
0 3 11
0 4 10
0 5 9
0 6 8
0 7 7
0 8 6
0 9 5
0 10 4
0 11 3
0 12 2
0 13 1
0 14 0
RRD
  • 1
  • 2
0

import java.util.Scanner;

public class ExampleMinNumber {

public static void main(String args[]) 
{

    System.out.println("enter number which comibnation of sum you want");
    Scanner input=new Scanner(System.in);
    int num=input.nextInt();
    for(int i=0;i<=3;i++)
    {
        int sum=0;
        for(int j=0;j<=3;j++)
        {
            for(int k=0;k<=3;k++)
            {
                sum=i+j+k;
                if(sum==num)
                {
                    System.out.println(+i+""+j+""+k);
                }

            }
        }
    }

    input.close();  
}
}
René Höhle
  • 26,716
  • 22
  • 73
  • 82