因式分解Java实现

package cun.zheng.weng.algorithmn.mathmatic;

import java.util.ArrayList;
import java.util.List;

public class FindAllDivisors {

List result = new ArrayList<>();

public List findAllDivisors(int n){
    if(n==1)
        return result;
    int i = 2;
    int r = n%2;
    while(true){
        if(r == 0){
            result.add(i);
            n = n/i;
            if(n == 1)
                return result;
            r=n%i;
            continue;
        } else {
            i++;
            r = n % i;
        }
    }
}

public static void main(String[] args){
    FindAllDivisors findAllDivisors = new FindAllDivisors();
    List list = findAllDivisors.findAllDivisors(1578);
    System.out.println(list);
}

}

你可能感兴趣的:(算法)