You are viewing a single comment's thread. Return to all 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; }
Seems like cookies are disabled on this browser, please enable them to open this website
Java Interface
You are viewing a single comment's thread. Return to all 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).