In this HackerRank Day 19: Interfaces problem solution The AdvancedArithmetic interface and the method declaration for the abstract divisorSum(n) method are provided for you in the editor below.

Complete the implementation of Calculator class, which implements the AdvancedArithmetic interface. The implementation for the divisorSum(n) method must return the sum of all divisors of n.

HackerRank Day 19: Interfaces problem solution

Problem solution in Python programming.

class Calculator(AdvancedArithmetic):
    def divisorSum(self, n):
        mylist = []
        for k in range(n):
            if n % (k+1) == 0:
                mylist.append(k+1)
        return sum(mylist)



Problem solution in Java programming.

//Write your code here
class Calculator implements AdvancedArithmetic{
    
    public int divisorSum(int n){
        int sum=0;
        for(int i=1;i<=n/2;i++){
            if(n%i==0){
                sum+=i;
            }
            
                     
        }
         return sum+n;
        
    }
}




Problem solution in C++ programming.

class Calculator : public AdvancedArithmetic
    {
    public:
        vector <int> hi;
        int divisorSum(int n)
            {
            for (int i = n;i > 0;i--)
                 {
                   if (n%i == 0)
                      hi.push_back(i);                
                 }
            int count = 0;
            for (auto & c : hi)
                {
                count+=c;
            }
            return count;
        }
};