In this lab, you will be writing your own method using the brute force algorithm, to solve a second degree polynomial. This method will take in four parameters, three coefficients, and one constant, and it will have the following signature:
public static String bruteForceEquationSolver(int one, int two, int three, int constant){}
The equation you are trying to solve is of the following format, given the parameters stated above:
(constant) = (one) * x + (two) * y + (three) * z X, Y, and Z are variables that you are finding solutions for.
For example, if you were to call the method with parameters (2, 3, 4, 42), you would be finding the solution for the equation 2x + 3y + 4z = 42.
Call the testBFES() method from main after completing this method to ensure it is working as expected.
Some specifications for the method:
Your method should try every possibility within the range (1-10) for each variable. Check possibilities using nested for loops, starting with x, then y, then z. Your method should return if a solution is found, and return a string with the solutions in the following format:
x: xSolution y: ySolution z: zSolution
There should be a space between the solution and the next variable, and each variable letter should be followed by a colon and a space.
If no solution is found, return the string “Solution not found.”.
Here's what I have so far and I don't know if I have the right set up or not.
public static String bruteForceEquationSolver(int one, int two, int three, int constant) { // student code here do { for (int i = 1; i <= 10; i++) { one *= i; for (int j = 1; j <= 10; j++) { two *= j; for (int k = 1; k <= 10; k++) { three *= k; } } } } while(one + two + three <= constant); return String.format("x:%d y:%d z:%d", one, two, three); } public static void testBFES() { System.out.println("Testing Brute Force Equation Solver"); String expected = "x: 2 y: 3 z: 4"; System.out.println("Expecting: " + expected); String actual = bruteForceEquationSolver(3, 4, 6, 42); System.out.println("Actual: " + actual); boolean correct = expected.equals(actual); System.out.println("Outputs equal? " + correct); } public static void testMT() { System.out.println("Testing Multiplication Table"); String expected = "1\t2\t3\t4\t\n2\t4\t6\t8\t\n3\t6\t9\t12\t\n"; System.out.print("Expecting:\n" + expected); String actual = multiplicationTable(3, 4); System.out.print("Actual:\n" + actual); boolean correct = expected.equals(actual); System.out.println("Outputs equal? " + correct); } 42 Answers
This with g_bor's proposed correction only solves your problem for integers from 1 to 10. You could gradually increase how large the integers can be, for instance by restricting |x|+|y|+|z| by an increasing bound. My idea how this could be done:
for(int bound = 0; bound >= 0; bound++){//stopping when overflowing for(int x = -bound; x <= bound; x++){//other x violates bound for(int y = -bound + Math.abs(x); y <= bound - Math.abs(x); y++){//force that |x|+|y| <= bound // Only test |x| + |y| + |z| = bound cases as smaller ones were already tested in previous iterations for(int z_sign = -1; z_sign <= 1; z_sign += 2){ int z = bound - Math.abs(x) - Math.abs(y);//|x|+|y|+|z|=bound z *= z_sign; if(one*x + two*y + three*z == constant){ //TODO format and return solution (x,y,z) } } } } } //TODO other return or exception if no solution found Note that this only finds all integer solution with |x|+|y|+|z| <= MAXINT. Thus, it may be wished to replace int by long for the bound variable.
Brute force Equation solver
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // equation format x1 + y1 = c1 // input will be 8 7 38 // x1 = 8, y1 = 7, c1 = 38 System.out.println("Enter first Equation:"); String input1 = scanner.nextLine(); String[] split = input1.split(" "); int x1 = Integer.parseInt(split[0]); int y1 = Integer.parseInt(split[1]); int c1 = Integer.parseInt(split[2]); System.out.println("Enter Second Equation:"); String input2 = scanner.nextLine(); split = input2.split(" "); int x2 = Integer.parseInt(split[0]); int y2 = Integer.parseInt(split[1]); int c2 = Integer.parseInt(split[2]); for (int x = -10; x <= 10; x++) { for (int y = -10; y <= 10; y++) { if (((x1 * x) + (y1 * y)) == c1 && ((x2 * x) + (y2 * y)) == c2) { System.out.println(String.format("x = %s, y = %s", x, y)); return; } } } System.out.println("There is no solution");