求一个数的质因数?

优化质因数分解算法

我面试遇到了,尝试写了一下。

public class Demo {
    public static void main(String[] args) {
        test(90);
    }`
    public static void test(int i){
        int index = 2;
        boolean next = true;
        for(int j = 1;j < i;j++){
            if(j%index == 0 && j!= 1){
                next =false;
            }
            if(next){
                if(i % index == 0){
                    System.out.println(index);
                    test(i/index);
                    break;
                }
            }
            index ++;   
        }   
    }
}

运行结果
这里写图片描述
但是有一个问题每次最后一次循环都要循环i次,如果数字大了就不好了,有什么解决方案吗?用math.sqrt(i)吗?