JAVA因式分解算法

    public static void main(String[] args) {
        f(60);
    }

    private static void f(int num) {
        for (int i = 2; i < num; i++) {
            while (num % i == 0) {
                System.out.println(i);
                num = num / i;
            }
        }
        if (num > 1) {
            System.out.println(num);
        }
    }

输出:

2
2
3
5

你可能感兴趣的:(Java笔记)