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];
answer(l,m,j,p);
return numbs;
}
}
////////////////////////
Thanks for the help