Hey I just found this site because I have been working on a program to make answer and create questions to the game 24. (24 is a game where players are given 4 numbers and by either adding, subtracting, multiplying and Dividing the given numbers the number at the end is either 24 or -24. However, each operation can only be used once. Ex. 4, 5, 7, 2 = 5*4+7-2=24) Parentheses can be uses. My first shot at making it was by using the brute force method where I made the program go through every possibility. The answer section works, but the question section gives sometimes gives me numbers that do not work in the answer section. Could anyone help me by either finding where my question section is wrong, or give me an idea of what a possible algorithm other than the brute force method would work for the 24 game.
////////////////////////////////

``````import java.util.Random;
public class Twentyfour {
double a;
double b;
double c;
double d;
double e=0;
double f=0;
boolean par=false;
public Twentyfour(boolean par){
//some people play the game with or without parentheses.
this.par=par;
}
public void answer(double a, double b, double c, double d) {
this.a=a;
this.b=b;
this.c=c;
this.d=d;
for (int i=0;i< 4; i++){
for (int v=0;v< 3; v++){
for (int x=0; x<2; x++){
if ((((a+b)-c)*d)==24||(((a+b)-c)*d)==-24)
System.out.println (a+"+"+b+"-"+c+"*"+d);
else if ((((a+b)-c)/d)==24||(((a+b)-c)/d)==-24)
System.out.println (a+"+"+b+"-"+c+"/"+d);
else if ((((a+b)/c)-d)==24||(((a+b)/c)-d)==-24)
System.out.println (a+"+"+b+"/"+c+"-"+d);
else if ((((a+b)/c)*d)==24||(((a+b)/c)*d)==-24)
System.out.println (a+"+"+b+"/"+c+"*"+d);
else if ((((a+b)*c)/d)==24||(((a+b)*c)/d)==-24)
System.out.println (a+"+"+b+"*"+c+"/"+d);
else if ((((a+b)*c)-d)==24||(((a+b)*c)-d)==-24)
System.out.println (a+"+"+b+"*"+c+"-"+d);
else if ((((a-b)+c)*d)==24||(((a-b)+c)*d)==-24)
System.out.println (a+"-"+b+"+"+c+"*"+d);
else if ((((a-b)+c)/d)==24||(((a-b)+c)/d)==-24)
System.out.println (a+"-"+b+"+"+c+"/"+d);
else if ((((a-b)/c)+d)==24||(((a-b)/c)+d)==-24)
System.out.println (a+"-"+b+"/"+c+"+"+d);
else if ((((a-b)/c)*d)==24||(((a-b)/c)*d)==-24)
System.out.println (a+"-"+b+"/"+c+"*"+d);
else if ((((a-b)*c)/d)==24||(((a-b)*c)/d)==-24)
System.out.println (a+"-"+b+"*"+c+"/"+d);
else if ((((a-b)*c)+d)==24||(((a-b)*c)+d)==-24)
System.out.println (a+"-"+b+"*"+c+"+"+d);
else if ((((a*b)+c)/d)==24||(((a*b)+c)/d)==-24)
System.out.println (a+"*"+b+"+"+c+"/"+d);
else if ((((a*b)+c)-d)==24||(((a*b)+c)-d)==-24)
System.out.println (a+"*"+b+"+"+c+"-"+d);
else if ((((a*b)-c)/d)==24||(((a*b)-c)/d)==-24)
System.out.println (a+"*"+b+"-"+c+"/"+d);
else if ((((a*b)-c)+d)==24||(((a*b)+c)+d)==-24)
System.out.println (a+"*"+b+"-"+c+"+"+d);
else if ((((a*b)/c)+d)==24||(((a*b)/c)+d)==-24)
System.out.println (a+"*"+b+"/"+c+"+"+d);
else if ((((a*b)/c)-d)==24||(((a*b)/c)-d)==-24)
System.out.println (a+"*"+b+"/"+c+"-"+d);
else if ((((a/b)+c)*d)==24||(((a/b)+c)*d)==-24)
System.out.println (a+"/"+b+"+"+c+"*"+d);
else if ((((a/b)+c)-d)==24||(((a/b)+c)-d)==-24)
System.out.println (a+"/"+b+"+"+c+"-"+d);
else if ((((a/b)-c)+d)==24||(((a/b)-c)+d)==-24)
System.out.println (a+"/"+b+"-"+c+"+"+d);
else if ((((a/b)-c)*d)==24||(((a/b)-c)*d)==-24)
System.out.println (a+"/"+b+"-"+c+"*"+d);
else if ((((a/b)*c)-d)==24||(((a/b)*c)-d)==-24)
System.out.println (a+"/"+b+"*"+c+"-"+d);
else if ((((a/b)*c)+d)==24||(((a/b)*c)+d)==-24)
System.out.println (a+"/"+b+"*"+c+"+"+d);
if (par){
f=c+d;
if (((a/b)*f)==24||(((a/b)*f))==-24)
System.out.println (a+"/"+b+"*("+c+"+"+d+")");
else if (((a/b)-f)==24||(((a/b)-f)==-24))
System.out.println (a+"/"+b+"-("+c+"+"+d+")");
else if (((a*b)-f)==24||(((a*b)-f)==-24))
System.out.println (a+"*"+b+"-("+c+"+"+d+")");
else if (((a*b)/f)==24||(((a*b)/f)==-24))
System.out.println (a+"*"+b+"/("+c+"+"+d+")");
else if (((a-b)*f)==24||(((a-b)*f)==-24))
System.out.println (a+"-"+b+"*("+c+"+"+d+")");
else if (((a-b)/f)==24||(((a-b)/f)==-24))
System.out.println (a+"-"+b+"/("+c+"+"+d+")");
f=c-d;
if (((a/b)*f)==24||(((a/b)*f))==-24)
System.out.println (a+"/"+b+"*("+c+"-"+d+")");
else if (((a/b)+f)==24||(((a/b)+f)==-24))
System.out.println (a+"/"+b+"+("+c+"-"+d+")");
else if (((a*b)+f)==24||(((a*b)+f)==-24))
System.out.println (a+"*"+b+"+("+c+"-"+d+")");
else if (((a*b)/f)==24||(((a*b)/f)==-24))
System.out.println (a+"*"+b+"/("+c+"-"+d+")");
else if (((a+b)*f)==24||(((a+b)*f)==-24))
System.out.println (a+"+"+b+"*("+c+"-"+d+")");
else if (((a+b)/f)==24||(((a+b)/f)==-24))
System.out.println (a+"+"+b+"/("+c+"-"+d+")");
f=c*d;
if (((a/b)-f)==24||(((a/b)-f))==-24)
System.out.println (a+"/"+b+"*("+c+"*"+d+")");
else if (((a/b)+f)==24||(((a/b)+f)==-24))
System.out.println (a+"/"+b+"+("+c+"*"+d+")");
else if (((a-b)+f)==24||(((a-b)+f)==-24))
System.out.println (a+"-"+b+"+("+c+"*"+d+")");
else if (((a-b)/f)==24||(((a-b)/f)==-24))
System.out.println (a+"-"+b+"/("+c+"*"+d+")");
else if (((a+b)-f)==24||(((a+b)-f)==-24))
System.out.println (a+"+"+b+"-("+c+"*"+d+")");
else if (((a+b)/f)==24||(((a+b)/f)==-24))
System.out.println (a+"+"+b+"/("+c+"*"+d+")");
f=c/d;
if (((a-b)*f)==24||(((a-b)*f))==-24)
System.out.println (a+"-"+b+"*("+c+"/"+d+")");
else if (((a-b)+f)==24||(((a-b)+f)==-24))
System.out.println (a+"-"+b+"+("+c+"/"+d+")");
else if (((a*b)+f)==24||(((a*b)+f)==-24))
System.out.println (a+"*"+b+"+("+c+"/"+d+")");
else if (((a*b)-f)==24||(((a*b)-f)==-24))
System.out.println (a+"*"+b+"-("+c+"/"+d+")");
else if (((a+b)*f)==24||(((a+b)*f)==-24))
System.out.println (a+"+"+b+"*("+c+"/"+d+")");
else if (((a+b)-f)==24||(((a+b)-f)==-24))
System.out.println (a+"+"+b+"-("+c+"/"+d+")");
f=b*c;
if (((a-f)/d)==24||(((a-f)/d))==-24)
System.out.println (a+"-("+b+"*"+c+")/"+d);
else if (((a-f)+d)==24||(((a-f)+d)==-24))
System.out.println (a+"-("+b+"*"+c+")+"+d);
else if (((a/f)+d)==24||(((a/f)+d)==-24))
System.out.println (a+"/("+b+"*"+c+")+"+d);
else if (((a/f)-d)==24||(((a/f)-d)==-24))
System.out.println (a+"/("+b+"*"+c+")-"+d);
else if (((a+f)/d)==24||(((a+f)/d)==-24))
System.out.println (a+"+("+b+"*"+c+")/"+d);
else if (((a+f)-d)==24||(((a+f)-d)==-24))
System.out.println (a+"+("+b+"*"+c+")-"+d);
f=b-c;
if (((a*f)/d)==24||(((a*f)/d))==-24)
System.out.println (a+"*("+b+"-"+c+")/"+d);
else if (((a*f)+d)==24||(((a*f)+d)==-24))
System.out.println (a+"*("+b+"-"+c+")+"+d);
else if (((a/f)+d)==24||(((a/f)+d)==-24))
System.out.println (a+"/("+b+"-"+c+")+"+d);
else if (((a/f)*d)==24||(((a/f)*d)==-24))
System.out.println (a+"/("+b+"-"+c+")*"+d);
f=b/c;
if (((a-f)*d)==24||(((a-f)*d))==-24)
System.out.println (a+"-("+b+"/"+c+")*"+d);
else if (((a-f)+d)==24||(((a-f)+d)==-24))
System.out.println (a+"-("+b+"/"+c+")+"+d);
else if (((a*f)+d)==24||(((a*f)+d)==-24))
System.out.println (a+"*("+b+"/"+c+")+"+d);
else if (((a*f)-d)==24||(((a*f)-d)==-24))
System.out.println (a+"*("+b+"/"+c+")-"+d);
else if (((a+f)*d)==24||(((a+f)*d)==-24))
System.out.println (a+"+("+b+"/"+c+")*"+d);
else if (((a+f)-d)==24||(((a+f)-d)==-24))
System.out.println (a+"+("+b+"/"+c+")-"+d);
f=b+c;
if (((a*f)/d)==24||(((a*f)/d))==-24)
System.out.println (a+"*("+b+"+"+c+")/"+d);
else if (((a*f)-d)==24||(((a*f)-d)==-24))
System.out.println (a+"*("+b+"+"+c+")-"+d);
else if (((a/f)-d)==24||(((a/f)-d)==-24))
System.out.println (a+"/("+b+"+"+c+")-"+d);
else if (((a/f)*d)==24||(((a/f)*d)==-24))
System.out.println (a+"/("+b+"+"+c+")*"+d);
}
e=c;
c=d;
d=e;
}
e=b;
b=c;
c=d;
d=e;
}
e=a;
a=b;
b=c;
c=d;
d=e;
}
}
public double[] question(){
while (true){
Random random = new Random();
int aa = random.nextInt(30);
double a=(double)aa;
int bb= random.nextInt(30);
double b=(double)bb;
int cc= random.nextInt(30);
double c=(double)cc;
int dd= random.nextInt(30);
double d=(double)dd;
double e =0;
double[] numbers = new double[4];
numbers[0]=a;
numbers[1]=b;
numbers[2]=c;
numbers[3]=d;
for(int p=0;p<4;p++){
if (numbers[p]==0){
numbers[p]=random.nextInt(30);
}
// Gives numbers that are negative
if (random.nextInt(2)==0){
numbers[p]=-numbers[p];
}
}
for (int i=0;i< 4; i++){
for (int v=0;v< 3; v++){
for (int x=0; x<2; x++){
if ((((a+b)-c)*d)==24||(((a+b)-c)*d)==-24)
return numbers;
else if ((((a+b)-c)/d)==24||(((a+b)-c)/d)==-24)
return numbers;
else if ((((a+b)/c)-d)==24||(((a+b)/c)-d)==-24)
return numbers;
else if ((((a+b)/c)*d)==24||(((a+b)/c)*d)==-24)
return numbers;
else if ((((a+b)*c)/d)==24||(((a+b)*c)/d)==-24)
return numbers;
else if ((((a+b)*c)-d)==24||(((a+b)*c)-d)==-24)
return numbers;
else if ((((a-b)+c)*d)==24||(((a-b)+c)*d)==-24)
return numbers;
else if ((((a-b)+c)/d)==24||(((a-b)+c)/d)==-24)
return numbers;
else if ((((a-b)/c)+d)==24||(((a-b)/c)+d)==-24)
return numbers;
else if ((((a-b)/c)*d)==24||(((a-b)/c)*d)==-24)
return numbers;
else if ((((a-b)*c)/d)==24||(((a-b)*c)/d)==-24)
return numbers;
else if ((((a-b)*c)+d)==24||(((a-b)*c)+d)==-24)
return numbers;
else if ((((a*b)+c)/d)==24||(((a*b)+c)/d)==-24)
return numbers;
else if ((((a*b)+c)-d)==24||(((a*b)+c)-d)==-24)
return numbers;
else if ((((a*b)-c)/d)==24||(((a*b)-c)/d)==-24)
return numbers;
else if ((((a*b)-c)+d)==24||(((a*b)+c)+d)==-24)
return numbers;
else if ((((a*b)/c)+d)==24||(((a*b)/c)+d)==-24)
return numbers;
else if ((((a*b)/c)-d)==24||(((a*b)/c)-d)==-24)
return numbers;
else if ((((a/b)+c)*d)==24||(((a/b)+c)*d)==-24)
return numbers;
else if ((((a/b)+c)-d)==24||(((a/b)+c)-d)==-24)
return numbers;
else if ((((a/b)-c)+d)==24||(((a/b)-c)+d)==-24)
return numbers;
else if ((((a/b)-c)*d)==24||(((a/b)-c)*d)==-24)
return numbers;
else if ((((a/b)*c)-d)==24||(((a/b)*c)-d)==-24)
return numbers;
else if ((((a/b)*c)+d)==24||(((a/b)*c)+d)==-24)
return numbers;
if (par){
f=c+d;
if (((a/b)*f)==24||(((a/b)*f))==-24)
return numbers;
else if (((a/b)-f)==24||(((a/b)-f)==-24))
return numbers;
else if (((a*b)-f)==24||(((a*b)-f)==-24))
return numbers;
else if (((a*b)/f)==24||(((a*b)/f)==-24))
return numbers;
else if (((a-b)*f)==24||(((a-b)*f)==-24))
return numbers;
else if (((a-b)/f)==24||(((a-b)/f)==-24))
return numbers;
f=c-d;
if (((a/b)*f)==24||(((a/b)*f))==-24)
return numbers;
else if (((a/b)+f)==24||(((a/b)+f)==-24))
return numbers;
else if (((a*b)+f)==24||(((a*b)+f)==-24))
return numbers;
else if (((a*b)/f)==24||(((a*b)/f)==-24))
return numbers;
else if (((a+b)*f)==24||(((a+b)*f)==-24))
return numbers;
else if (((a+b)/f)==24||(((a+b)/f)==-24))
return numbers;
f=c*d;
if (((a/b)-f)==24||(((a/b)-f))==-24)
return numbers;
else if (((a/b)+f)==24||(((a/b)+f)==-24))
return numbers;
else if (((a-b)+f)==24||(((a-b)+f)==-24))
return numbers;
else if (((a-b)/f)==24||(((a-b)/f)==-24))
return numbers;
else if (((a+b)-f)==24||(((a+b)-f)==-24))
return numbers;
else if (((a+b)/f)==24||(((a+b)/f)==-24))
return numbers;
f=c/d;
if (((a-b)*f)==24||(((a-b)*f))==-24)
return numbers;
else if (((a-b)+f)==24||(((a-b)+f)==-24))
return numbers;
else if (((a*b)+f)==24||(((a*b)+f)==-24))
return numbers;
else if (((a*b)-f)==24||(((a*b)-f)==-24))
return numbers;
else if (((a+b)*f)==24||(((a+b)*f)==-24))
return numbers;
else if (((a+b)-f)==24||(((a+b)-f)==-24))
return numbers;
f=b*c;
if (((a-f)/d)==24||(((a-f)/d))==-24)
return numbers;
else if (((a-f)+d)==24||(((a-f)+d)==-24))
return numbers;
else if (((a/f)+d)==24||(((a/f)+d)==-24))
return numbers;
else if (((a/f)-d)==24||(((a/f)-d)==-24))
return numbers;
else if (((a+f)/d)==24||(((a+f)/d)==-24))
return numbers;
else if (((a+f)-d)==24||(((a+f)-d)==-24))
return numbers;
f=b-c;
if (((a*f)/d)==24||(((a*f)/d))==-24)
return numbers;
else if (((a*f)+d)==24||(((a*f)+d)==-24))
return numbers;
else if (((a/f)+d)==24||(((a/f)+d)==-24))
return numbers;
else if (((a/f)*d)==24||(((a/f)*d)==-24))
return numbers;
f=b/c;
if (((a-f)*d)==24||(((a-f)*d))==-24)
return numbers;
else if (((a-f)+d)==24||(((a-f)+d)==-24))
return numbers;
else if (((a*f)+d)==24||(((a*f)+d)==-24))
return numbers;
else if (((a*f)-d)==24||(((a*f)-d)==-24))
return numbers;
else if (((a+f)*d)==24||(((a+f)*d)==-24))
return numbers;
else if (((a+f)-d)==24||(((a+f)-d)==-24))
return numbers;
f=b+c;
if (((a*f)/d)==24||(((a*f)/d))==-24)
return numbers;
else if (((a*f)-d)==24||(((a*f)-d)==-24))
return numbers;
else if (((a/f)-d)==24||(((a/f)-d)==-24))
return numbers;
else if (((a/f)*d)==24||(((a/f)*d)==-24))
return numbers;
}
e=c;
c=d;
d=e;
}
e=b;
b=c;
c=d;
d=e;
}
e=a;
a=b;
b=c;
c=d;
d=e;
}
}
}
//This runs the game with the numbers created in the question to see if it works
public double[] check(){
double l;
double m;
double j;
double p;
double[] numbs = new double[4];
numbs= question();
l=numbs[0];
m=numbs[1];
j=numbs[2];
p=numbs[3];
return numbs;

}
}``````

////////////////////////
Thanks for the help

think before you code...
how'd you do it if you were given the numbers, a pencil, and a piece of paper?

Would you just try random combinations and see if they yield the desired result? Of course not.
You'd use logic.
For example you'd determine which were the two largest numbers and start multiplying those. If that yields a result that's higher than what you're looking for, it's probably already impossible to get where you want to go. etc. etc.

line 201-209 you lose the consistency between the content of the array "numbers" and the values of variables "a,b,c,d".

Thanks that was the problem

lol @jwenting it's all very easy to say but in reality such questions are still best solved by just calculating through all permutations. Programs are not that intelligent to know which permutations not to calculate. Not to mention I don't think you understood the question at all. Even if the number goes "over", you can obviously subtract as well as divide etc. So there's really no way of knowing besides just calculating it all out. Human beings at most do an optimization to cut off unlikely search branches. That doesn't mean such a search is not good. Also see https://rosettacode.org/wiki/24_game/Solve

SZJX: you do realize this thread is over five years old, and nobody is still following it, right?

besides: "best solved by just calculating through all permutations". yes, wasting the system's resources is always "thÃ© way to go".
if you can minimize the number of permutations, you should. if you can and don't do it, you're a lousy developer.

Well. I'm following it again now...

In my opinion, I would rather waste a few miliisecs of CPU than try to debug some highly optimised code that only works most of the time. CPU is cheap cheap cheap, programmers are expensive, bugs are very expensive, so the simplest program is frequently the best.

(with obvious exceptions - graphics drivers, very large servers, weather forcasting, certain kinds of scientific data analysis etc)

cpu is cheap, sure, but the lines of code to go over from time to time make it quite hard just to get a 'complete overview'.

If we're talking ten lines, I would agree in saying 'optimization is not needed or beneficial here', but a code such as the one above could do with some improvements.