Solving a difficult equation with two variables in Java
-
27-10-2019 - |
Question
My assignment says to do the following:
Search2: Search for a solution to x*x + y*y - 12x -10y + 36 = 0. Search from 0 to 10 in both x and y, searching every y value before moving to the next x. Print the first three solutions found. (Note - a labelled break is handy here!)
I can't figure out the logic for this. I think I have to use more than 2 loops but not sure.
This is what I have so far (It just repeats (6,0)):
for (int j = 0; j <= 10; j++) {
for (int i = 0; i <= 10; i++) {
while (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
System.out.println("(" + i + ", " + j + ")");
}
}
}
UPDATE
Here is the solution:
int t = 0;
for (int i = 0; i <= 10; i++) {
if (t == 3) {
break;
}
for (int j = 0; j <= 10; j++) {
if (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
System.out.println("(" + i + ", " + j + ")");
t++;
}
}
}
Solution
Not a bad attempt. Because you're so close, I'll show you a working solution. Basically, you need to do three things:
- Change
while
toif
- Use a variable to count the number of times you find the solution, so you can stop at three
- Add a label so you can break out of the outer loop from within the inner loop
I also recommend you use variable names the same as the problem - ie x
and y
- for clarity.
int count = 0;
outerLoop:
for (int y = 0; y <= 10; y++) {
for (int x = 0; x <= 10; x++) {
if (x * x + y * y - 12 * x - 10 * y + 36 == 0) {
System.out.println("(" + x + ", " + y + ")");
if (++count == 3)
break outerLoop;
}
}
}
When executed, this code produces:
(6, 0)
(3, 1)
(9, 1)
Sorry for spoon-feeding you, but part of the lesson here is good coding style and practice.
OTHER TIPS
You're using an extra while
loop that runs indefinetly.
while (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0) {
System.out.println("(" + i + ", " + j + ")");
}
The first time this evaluates to true - i.e. when it reaches (6,0) - it will keep running because i
and j
are not modified inside.
You need to replace it with an if
.
Take a good look at the inner while loop you have. Once the solution to the equation is found, i
and j
never change, and the formula always evaluates to 0
, resulting in an infinite loop.
It might also be wise to rename i
and j
to x and y, for clarity. You're mostly on the right track though. Don't forget you only have to print the first three solutions.
I wont help you much since this is a pretty simple concept, but think about what you need to loop through, it might make it easier to instead use x and y instad of i or j. Also your printing only (6,0) because thats exactly what you told it to do with the while loop.
One hint, your while loop should have a statement that stops its function, lets say if x < 4, if it ever is greater or = to than it will continue outside the loop.
public class EquationSolver {
public static void main(String[] args) {
int found = 0;
searchSolutions:
for (int y = 0; y <= 10; y++) {
for (int x = 0; x <= 10; x++) {
if (((x * x) + (y * y) - (12 * x) - (10 * y) + 36) == 0) {
System.out.println("(" + x + ", " + y + ")");
found ++;
if (found == 3) {
break searchSolutions;
}
}
}
}
}
}
Long time I last wrote some homework of that sort...just for the fun of it :)
public class Main {
public static void main(String[] args) {
int[] range = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
boolean isSolutionFound = Boolean.FALSE;
int solCounter = 0;
searchSolutions:
for (Integer y : range) {
for (Integer x : range) {
isSolutionFound = checkForSolution(x, y);
if (isSolutionFound) {
printSolution(x, y);
solCounter++;
}
if (solCounter == 3)
break searchSolutions;
}
}
}
private static void printSolution(Integer x, Integer y) {
System.out.println(x + "," + y); // use some fancy formatting instead
}
private static boolean checkForSolution(int x, int y) {
if (x * x + y * y - 12 * x - 10 * y + 36 == 0)
return true;
else
return false;
}
}
for (int j = 0; j <= 10; j++)
{
for (int i = 0; i <= 10; i++)
{
if (((i * i) + (j * j) - (12 * i) - (10 * j) + 36) == 0)
{
System.out.println("(" + i + ", " + j + ")");
return;
}
}
}
It is import to remember the return;
part as you would otherwise still search for solutions although you already found one. If you wan't more solutions, then you should should omit the ´return´ statement.