We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
classMyCalculatorimplementsAdvancedArithmetic{publicintdivisor_sum(intn){intsum=0;intsqrt=(int)Math.sqrt(n);for(inti=1;i<=sqrt;i++){if(n%i==0){// if "i" is a divisorsum+=i+n/i;// add both divisors}}/* If sqrt is a divisor, we should only count it once */if(sqrt*sqrt==n){sum-=sqrt;}returnsum;}}
Let me know if you have any questions.
Cookie support is required to access HackerRank
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 →
Efficient O(n^(1/2)) solution
From my HackerRank solutions.
Runtime: O(n^(1/2))
Space Complexity: O(1)
Let me know if you have any questions.