M. kay M. kay - 2 months ago 8
Java Question

Unreachable code + how to go through all combinations

I want to search through all values from -100 to 100 for a,b and c to find when a√2 - b = c√3 . I know that it's true when a,b and c are 0, but I just wanted to make a program for it.

I don't know what to do in order to get my program to search through every possible combination of a,b and c from -100 to 100. The program that I just created made a,b and c increase by 1 on each iteration, so I couldn't get all of the combinations. I tried to increase the amount of combinations by using "continue;", but it created an "unreachable code" error. This still wouldn't give me all possible combinations.

I also tried to increase a,b and c by 0.001 after every iteration of the loop, by making them doubles. This should have stopped at 0.000..... but instead it continued forever. I found out that this was because some random small number like 9.4524 x 10^-8 (different on every iteration) was added to the end of a,b and c.

This is my code:

public static void main(String[] args) {
int a = -100;
int b = -100;
int c = -100;

while ((a*Math.sqrt(2)) - b != (c*Math.sqrt(3))) {
if (a == 100){
return;
} else{
a++;
System.out.println("a = " + a);
continue;
}

if (b == 100){
return;
} else{
b++;
System.out.println("b = " + b);
continue;
}

if (c == 100){
return;
} else {
c++;
System.out.println("c = " + c);
continue;
}
}
}


What could I do to solve the problems above? Any help would be much appreciated.

Thanks.

Answer

Your problem is with the way you're incrementing through the combinations of a, b, and c. You want to check for each a, for each b, for each c whether a√2 - b = c√3. Also, to speed up your code, you can cache the values of √2 and √3, since they are constants being recalculated in each iteration. Also, like you mentioned in your question, you can run into rounding errors in floating point arithmetic. Therefore, it would be wise to use the static compare method of the Double class. You can read about that here.

This code below will print out all the integer combinations of a, b, and c that satisfy the equation a√2 - b = c√3. If you're just looking for one solution, maybe take the nested for loops and put them in their own method, then when the if condition is met, return that one solution. You could also make it return a list of solutions by adding each solution that meets the if condition to an ArrayList, then returning after the loops complete.

public class SurdsMain {
    public static void main(String[] args) {
        final double SQRT2 = Math.sqrt(2);
        final double SQRT3 = Math.sqrt(3);

        for(int a = -100; a <= 100; a++) {
            for(int b = -100; b <= 100; b++) {
                for(int c = -100; c <= 100; c++) {
                    if(Double.compare(a * SQRT2 - b, c * SQRT3) == 0) {
                        System.out.println("a = " + a + "; b = " + b + "; c = " + c);
                    }
                }
            }
        }
    }
}