The exact link to the question : https://www.spoj.com/problems/DIVSUM2/
Input: An integer stating the number of test cases (equal to 500), and that many lines follow, each containing one integer between \$1\$ and \$10^{16}\$ inclusive.
Output: Print one integer, the sum of the proper divisors of the number.
An easier version of this question exists in which the constraints are relaxed.
My code works for the easier version , but exceeds the time limit for the harder one.
Example of approach: \$36=2^2 \times 3^2\$. So sum of proper factors = $$(2^0+2^1+2^2)\times(3^0+3^1+3^2)-36=55$$
My code:
#include<iostream>
long long sum_proper_divisors(long long n)
{
long long sum=1,x=n;
for(long long i=2;i*i<=n;i++)
{
long long current_sum=1,current_term=1;
while(n%i==0)
{
n/=i;
current_term*=i;
current_sum+=current_term;
}
sum*=current_sum;
}
if(n>=2){
sum*=(n+1);
}
return (sum-x);
}
int main()
{
long long q;
std::cin>>q;
while(q--)
{
long long n;
std::cin>>n;
std::cout<<sum_proper_divisors(n)<<std::endl;
}
}