简体   繁体   中英

Can anyone tell me why my outer for loop doesn't increment the variable?

I use Eclipse and when I debug and step through the code my outer loop doesn't increment and the root always stays as 2. Can anyone tell me why? The top comment explains what I'm trying to accomplish.

public class First_120_Numbers {
    /*A Leyland number sequence is a series of numbers of the formxy + yxwhere x and y are integers greater than 1.    
     * They are named after the mathematician Paul Leyland. The first few Leyland numbers are  
     *8, 17, 32, 54, 57, 100, 145, 177, 320, 368, …
     *Take 145 as an example where it is x^y+y^x = 34+43
     *By considering the terms in the Leyland sequence find the sum of the first 20 numbers in the Leyland sequence.*/
    public static void main(String[] args) {
        double root, expo, prodX, prodY, leySum1 = 0, leySum2 = 0;
        root = 2; // Leyland numbers must be greater than 1
        expo = 2;
        for (; root <= 20; root++) {
            for (; expo <= 20; expo++) {
                prodX = Math.pow(root, expo); //raises root to expo
                prodY = Math.pow(expo, root);// raises expo to root
                leySum1 = prodX + prodY;
                leySum2 += leySum1;
                System.out.println(leySum1);
            }
        }
        System.out.println("The sum of the leyland numbers " 
                + "up to 20 is " + leySum2);
    }
}

You are not correct. root variable gets incremented every time but I guess you forgot to initialize the variable expo each time the outer loop iterates.

Just think, when the outer loop finishes its first iteration (for root = 2 ), the value of expo becomes 21 and since you are not initializing it to 2 again, the inner loop is not executing for rest of the iteration of the outer loop.

To better understand execture the following code snippet and see what happens!

for (root = 2; root <= 20; root++) {
    System.out.print(root + " --> ");
    for (expo = 2; expo <= 20; expo++) {
        // your code goes here
        System.out.print(expo + " ");
    }
    System.out.println("");
}

It outputs:

2.0 --> 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0 
3.0 --> 
4.0 --> 
5.0 --> 
6.0 --> 
7.0 --> 
8.0 --> 
9.0 --> 
10.0 --> 
11.0 --> 
12.0 --> 
13.0 --> 
14.0 --> 
15.0 --> 
16.0 --> 
17.0 --> 
18.0 --> 
19.0 --> 
20.0 --> 

To solve your problem, you can update your code as follows.

for (root = 2; root <= 20; root++) {
    for (expo = 2; expo <= 20; expo++) {
        prodX = Math.pow(root, expo); //raises root to expo
        prodY = Math.pow(expo, root);// raises expo to root
        leySum1 = prodX + prodY;
        leySum2 += leySum1;
        System.out.println(leySum1);
    }
}

See Live Demo of your code.

You need to re-initialize expo variable for each run of outer loop.

This this

double root, expo, prodX, prodY, leySum1 = 0, leySum2 = 0;
        for (root=2; root <= 20; root++) {
            for (expo=2; expo <= 20; expo++) {
                prodX = Math.pow(root, expo); //raises root to expo
                prodY = Math.pow(expo, root);// raises expo to root
                leySum1 = prodX + prodY;
                leySum2 += leySum1;
                System.out.println(leySum1);
            }
        }
        System.out.println("The sum of the leyland numbers " 
                + "up to 20 is " + leySum2);

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