• + 0 comments

    You can optimize by going only up to sqrt(n) and adding both the factors at once (similar principal when looking for Prime numbers).

        public int divisor_sum(int n) {
            if(n==1) return 1;
            int sum=0;
            for(int i=1; i * i <= n; i++) {
                if(n%i==0) {
                    sum += i;
                    sum += n/i; //other factor of a * b = n
                }
            }
            return sum;
        }