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.
  • Hackerrank Home
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Tutorials
  3. 30 Days of Code
  4. Day 19: Interfaces
  5. Discussions

Day 19: Interfaces

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial

Sort 592 Discussions, By:

votes

Please Login in order to post a comment

  • saxtorff
    5 years ago+ 0 comments

    No Python?! :-(

    105|
    Permalink
  • Bob_Roebling
    5 years ago+ 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;
        }
    }
    
    36|
    Permalink
  • dillingerjames
    2 years ago+ 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
    
    16|
    Permalink
  • RodneyShag
    4 years ago+ 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;
    }
    
    13|
    Permalink
  • manmohansaraswat
    1 year ago+ 0 comments

    Here is Mine Code in Python :

    def divisorSum(self, n):
        sum = 0
        for i in range(1,n+1):
            if n % i == 0:
                sum += i
        return sum
    
    7|
    Permalink
Load more conversations

Need Help?


View tutorial
View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature