简体   繁体   中英

Solving an equation using incremental nested for-loop

I am trying to solve an equation using nested forloops.

I am trying to solve for (i,j,k) in the equation (-25)i + (14)j + (-8)k = -77. Norm is -77.

I have been trying to debug it but cant find the problem with my algorithm.

Here is my code:

int newx = -20; int newy = -20; int newz = -20;
test = true;
while(test){
    for(int i = 0; i < 20; i++){
        if((f[0]*(newx+i) + f[1]*newy + f[2]*newz) == norm){
            System.out.println(f[0]*(newx+i) + f[1]*newy + f[2]*newz);
        } else {
            for(int j = 0; j < 20; j++){
                if((f[0]*(newx) + f[1]*(newy+j) + f[2]*newz) == norm){
                    System.out.println((f[0]*(newx) + f[1]*newy+j + f[2]*newz));
                } else {
                    for(int k = 0; k < 20; k++){
                        if((f[0]*(newx) + f[1]*(newy) + f[2]*(newz+k)) == norm){
                            System.out.println((f[0]*(newx) + f[1]*newy + f[2]*newz+k));
                        }
                    }
                }
            }
        }
    }
}

Your approach in general is convoluted. You're attempting to obtain the value of i , j and k . So keep it simple!

First, you're going to need to test every possible combination, so a triple nested for loop makes sense. Then, you're going to need to run the calculation and test the output. If the output is -77 , you've found one solution for i , j and k . So breaking this down into stages we've got.

  • Test every value for i , j and k .
  • Plug i , j and k into the formula.
  • If the output of the formula is -77 , print the results.

So now we know the stages , let's turn that into some code.

for(int i = 0; i < 20; i++) {
  for(int j = 0; j < 20; j++) {
     for(int k = 0; k < 20; k++) {
        // Go through each value.
        int output = (-25 * i) + (14 * j) + (-8 * k);
        // Plug the values into the formula.
        if(output == -77) {
            // Test and output.
            System.out.println("i = " + i + ", j = " + j + ", k = " + k);
        }
     }
  }
}

Now, you do not know how many solutions you are going to get here, so I would create another class called Solution .

public class Solution {
    private int i;
    private int j;
    private int k;

    // Appropriate constuctor and getters.
}

That way, when you need to save it, you can say.

List<Solution> mySolutions = new ArrayList<Solution>();

// Nested for loops.

mySolutions.add(new Solution(i, j, k)); // Provided that you wrote the constructor

And you can override the toString method in the Solution object to print out a reasonable output..

public String toString() {
    return "i =" + i + ", j =" + j + ", k =" + k;
}

So when you output, it will look something like..

i = 1, j = 2, k = 10
i = 1, j = 6, k = 17
i = 3, j = 1, k = 2

Check out..

  • I've made a simple version in IDEOne to get you started.

You are evaluating the inner if conditions incorrectly, eg:

for(int j = 0; j < 20; j++){
    if((f[0]*(newx) + f[1]*(newy+j) + f[2]*newz) == norm){
        System.out.println((f[0]*(newx) + f[1]*newy+j + f[2]*newz));
    }
    [...]
}

you forgot to add i to newX , it should be f[0] * (newx + i) . This applies to the if condition, as well as the println(...) . If you correct those bugs, you find a solution.

To get rid of the endless-loop:

  • remove the while loop
  • add && test to each for loop condition
  • add test = false; in each if -body

(This solution is quick and dirty, but I will let you figure out, how to solve it in a more appropiate fashion).

For a more elegant, overall solution please take a look at christoper's answer . There you find a (from a software engineer's point of view) cleaner solution, as well as some extensions you might want to have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM