Fastest way to find all the divisors of a large number

▼魔方 西西 提交于 2021-02-08 07:35:36

问题


I am facing a problem when I want to get all the divisors of a large number i.e n=10^12. There is a method, which checks all possible numbers less than square root of given number n.

for(int i=1; i<=sqrt(n); ++i){
    if(n%i==0){
        factors.push_back(i);
        if(i!=n/i) factors.push_back(n/i);
    }
}

But when n is very large i.e 10^12 then it needs 10^6 iterations which is very slow.

Is there any faster way when I have all prime divisors of given n such as given number is 48. Then prime factorization of 720 is 2^4 * 3^2 * 5.

All the divisors of 720 are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 30, 36, 40, 45, 48, 60, 72, 80, 90, 120, 144, 180, 240, 360, 720.

I have already searched for faster way than O(sqrt(n)). But haven't found.

来源:https://stackoverflow.com/questions/40939369/fastest-way-to-find-all-the-divisors-of-a-large-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!