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.

# Day 19: Interfaces

# Day 19: Interfaces

saxtorff + 0 comments No Python?! :-(

Bob_Roebling + 0 comments C# - Just kept it simple, I'm sure the performance geeks are screaming.

public class Calculator : AdvancedArithmetic { public int divisorSum(int n) { int sum = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) sum += i; } return sum; } }

RodneyShag + 0 comments ### Efficient O(n^(1/2)) solution

From my HackerRank solutions.

Runtime: O(n^(1/2))

Space Complexity: O(1)public int divisorSum(int n) { int sum = 0; int sqrt = (int) Math.sqrt(n); // Small optimization: if n is odd, we can't have even numbers as divisors int stepSize = (n % 2 == 1) ? 2 : 1; for (int i = 1; i <= sqrt; i += stepSize) { if (n % i == 0) { // if "i" is a divisor sum += i + n/i; // add both divisors } } // If sqrt is a divisor, we should only count it once if (sqrt * sqrt == n) { sum -= sqrt; } return sum; }

dillingerjames + 0 comments Here is a simple solution written in Python 3. Surprisingly, many other solutions I see don't realize you don't need to iterate though each number - only the first half.

`def divisorSum(self, n): if n == 1: return 1 else: factor_sum = 1 + n for i in range(2, n//2 + 1): if n % i == 0: factor_sum += i return factor_sum`

krylovsentry + 0 comments With test case #6 something wrong. Excpected output just a number, without any sentences about implementation.

Load more conversations

Sort 508 Discussions, By:

Please Login in order to post a comment