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.