Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to find combinations of integers and any operand to obtain a fixed result

I am writing a program that takes 4 numbers as input and then attempts to see if a combination of addition subtraction division and multiplication of the four numbers can make them equal to 24. My way was creating a method for every single possible combination of the four numbers and the four operands, which is a bit verbose. Here is an example of 2 methods.

public static boolean add(int a, int b, int c, int d) {
    boolean adBool;
    adBool = a + b + c + d == 24;
    return adBool;
}

public static boolean sub1(int a, int b, int c, int d) {
    boolean subBool1;
    subBool1 = a - b - c - d == 24;
    return subBool1;
}

Then in my Main I do create a while loop for each method that if the method returns true, will print that the method it stopped on, was the solution. Here is an example.

while (add(num1, num2, num3, num4)) {
    System.out.println("Your solution is "
            + num1 + " + " + num2 + " + " + num3 + " + " + num4
            + " = 24\nCongratulations!");
    break;

}
while (sub1(num1, num2, num3, num4)) {
    System.out.println("Your solution is "
            + num1 + " - " + num2 + " - " + num3 + " - " + num4
            + " = 24\nCongratulations!");
    break;
}

Is there a way to store operands like + and - so that I can put them in an array and just use some nested for loops to write this?

like image 321
Gabe Spound Avatar asked Sep 26 '22 08:09

Gabe Spound


1 Answers

Assuming the operands are fixed, you can create a generator which dumps out the possible operators, and pass them to an evaluator to determine if they are true.

while (generator.hasNext()){
    Operators ops = generator.getNext();
    if evaluatesTo(operand1, operand2, operand3, operand4, 24, ops){
       // print it
    }
}

A simple generator could be done like this:

List<String> list = new ArrayList<String>();
list.add("+++");
list.add("++-");
...
Iterator generator = list.iterator();

where generator implements the java.util.Iterator interface that initializes with all the operators (+-*/) and dumps out all the permutations of size 3.

The evalutesTo method simply calculates it:

public boolean (int operand1, int operand2, int operand3, int operand4, int total, Operators ops ){
    // calculate operand1 "ops.get(0)" operand2 "ops.get(1)" operand3 "ops.get(2)" operand4  == total
}

So if ops is [+-/] it would check

if (operand1 + operand2 - operand3 / operand4 == 24) return true;

I should add there's all kinds of efficiencies you can add later, but you're question is how you can do this with a better strategy. There are a few comments on details from other users, but I wouldn't worry about it now. First you need to set up this kind of framework, then you can worry about the details. Most key to this, is that you do not need to make 100s of similar looking methods.

like image 66
ergonaut Avatar answered Sep 29 '22 06:09

ergonaut