简体   繁体   中英

Dead code warning in the i--

I'm going to try to define function which return greatest common divisor. Now I'm just meeting some obstacle. Why do dead code warning appear in the i--? I can feel nothing so please let me know what is wrong.

public class Main {

    public static int function(int a, int b, int c) {
        int min;
        if (a > b) {
            if (b > c) {
                min = c;
            } else {
                min = b;
            }
        } else {
            if (a > c) {
                min = c;
            } else {
                min = a;
            }
        }
        for (int i = min; i > 0; i--) {
            if (a % i == 0 && b % i == 0 && c % i == 0) {
                return i;
            }
            return -1;
        }
        return 0;
    }

    public static void main(String[] args) {

        System.out.println("(400, 300, 750)의 최대 공약수 : " + function(400, 300, 750));

    }

}

The for loop has an if block where you are returning the greatest common divisor if found. If not you are returning -1. So the loop will never continue and "i--" will never be executed. That is why it's a dead code. Remove "return -1", it should work properly.

Stepping through the code in your debugger is often the quickest way to find these bugs. However, you could make the code much faster by only checking values which are a factor of the min value. This reduces the number of iterations dramatically.

public class Main {

    public static int function(int a, int b, int c) {
        int min;
        if (a > b) {
            if (b > c) {
                min = c;
            } else {
                min = b;
            }
        } else {
            if (a > c) {
                min = c;
            } else {
                min = a;
            }
        }
        for (int i = min; i > 0; i--) {
            if (a % i == 0 && b % i == 0 && c % i == 0) {
                System.err.println("Iterations " + (min + 1 - i));
                return i;
            }
        }
        return 0;
    }

    public static long gcd(long a, long b, long c) {
        long min = Math.min(a, Math.min(b, c));
        for (int j = 1, max = (int) Math.sqrt(min); j <= max; j++) {
            long i = min / j;
            if (a % i == 0 && b % i == 0 && c % i == 0) {
                System.err.println("Iterations: " + j);
                return i;
            }
        }
        return 1;
    }

    public static void main(String[] args) {
        System.out.println("(400, 300, 750)의 최대 공약수 : " + function(400, 300, 750));
        System.out.println("(400, 300, 750)의 최대 공약수 : " + gcd(400, 300, 750));
    }
}

prints

(400, 300, 750)의 최대 공약수 : 50
(400, 300, 750)의 최대 공약수 : 50
Iterations 251
Iterations: 6

In your approach, it has to consider all the factors from 300 to 50 (251 values). But considering only the factors of 300 ie 300/1, 300/2, 300/3, 300/4, 300/5, 300/6 (6 values) it is much faster.

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