Saturday, April 28, 2012

Divisor Summation

( http://www.spoj.pl/problems/DIVSUM/ )
#include <stdio.h>
#include <math.h>
int main() {
    long long int n,i,t,d,k,l;
    scanf("%lld",&t);
    while(t--) {
        d = 0;
        scanf("%lld",&n);
        k = sqrt(n);
        for(i = 1; i <= k; i++) {
            if((n%i) == 0) {
                d = d+i;
                l = n/i;
                if(i != l)
                    d = d+l;
            }
        }
        printf("%lld\n",d-n);
    }
    return 0;
}

2 comments: