Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an "all these numbers are different" condition in Java?

OK, I have this problem to solve but I can’t program it in Java correctly. See the picture below, you’ll see a 6 pointed star were every point and intersection of lines is a letter.

The assignment is to position the numbers 1 to 12 in such a way that the sum of all lines of four balls is 26 and the sum of all the 6 points of the star is 26 as well. This comes down to:

  • (A+C+F+H==26)
  • (A+D+G+K==26)
  • (B+C+D+E==26)
  • (B+F+I+L==26)
  • (E+G+J+L==26)
  • (H+I+J+K==26)
  • (A+B+E+H+K+L==26)

So I started programming a program that would loop through all options brute forcing a solution. The loop is working, however, it now shows solutions where one number is used more than once, which is not allowed. How can I make it in the code that it also checks whether all variables are different or not?

if ((A!= B != C != D != E != F != G != H != I != J != K != L)

I tried the above, but it doesn't work, because it says:

incomparable types: boolean and int.

How can I make a check within 1 or a small statement for whether or not all the numbers are different?

(instead of making a nested 12*12 statement which checks every variable combination)

This is my code so far:

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

    for(int A = 1; A < 13; A++){
     for(int B = 1; B < 13; B++){
      for(int C = 1; C < 13; C++){
       for(int D = 1; D < 13; D++){
        for(int E = 1; E < 13; E++){
         for(int F = 1; F < 13; F++){
          for(int G = 1; G < 13; G++){
           for(int H = 1; H < 13; H++){
            for(int I = 1; I < 13; I++){
             for(int J = 1; J < 13; J++){
              for(int K = 1; K < 13; K++){
               for(int L = 1; L < 13; L++){
                if ((A+C+F+H==26) && (A+D+G+K==26) && (B+C+D+E==26) && (B+F+I+L==26) && (E+G+J+L==26) && (H+I+J+K==26) && (A+B+E+H+K+L==26)){
                 if ((A= C != D != E != F != G != H != I != J != K != L)){
                 System.out.println("A: " + A);
                 System.out.println("B: " + B);
                 System.out.println("C: " + C);
                 System.out.println("D: " + D);
                 System.out.println("E: " + E);
                 System.out.println("F: " + F);
                 System.out.println("G: " + G);
                 System.out.println("H: " + H);
                 System.out.println("I: " + I);
                 System.out.println("J: " + J);
                 System.out.println("K: " + K);
                 System.out.println("L: " + L);
                 }
                }
               }
              }
             }
            }
           }
          }
         }
        }
       }
      }
     }
    }
   }

}
like image 404
Javaaaa Avatar asked Dec 06 '10 11:12

Javaaaa


2 Answers

If I get it correctly, you want to check if all A to L are unique. So just put them in a set and find the size of the set:

if ((new HashSet<Integer>(
        Arrays.asList(A, B, C, D, E, F, G, H, I, J, K, L)))
    .size() == 12) {
    //do your stuff
}
like image 72
Abhinav Sarkar Avatar answered Sep 20 '22 06:09

Abhinav Sarkar


I strongly advise using recursion instead, which would vastly simplify the code. Do something like this:

function generate(set used, array list):
  if list.size() == 12:
    if list matches criteria:
      yield list as solution
  else:
    for next = 1; next < 13; next++:
      if next not in used:
        used.add(next)
        generate(used, list + next)
        used.remove(next)

However, to answer you question directly: You can throw all the values into a set and check that it's size is equal to the number of items you threw in. This works because a set will count duplicates as one.

like image 41
moinudin Avatar answered Sep 20 '22 06:09

moinudin