In this HackerRank Day 19 Interfaces 30 days of code problem set, we need to take an integer input and then we need to subtract that integer and then add all the numbers and print the sum of them on the output screen.
Problem solution in Python 2 programming.
class Calculator(AdvancedArithmetic): def divisorSum(self, n): return divisors(n) def divisors(n): c = 1 s = 0 while c <= n: if n % c == 0: s += c c += 1 return s
Problem solution in Python 3 programming.
class Calculator(AdvancedArithmetic): def divisorSum(self, n): temp = [] for i in range(1, n+1): if n%i == 0: temp.append(i) return sum(temp)
Problem solution in java programming.
class Calculator implements AdvancedArithmetic{ public int divisorSum(int n){ // n has no divisors other than itself if(n == 1){ return n; } // Find and sum divisors: int half = n/2; int sum = n; do{ if(n % half == 0){ sum += half; } }while( half-- > 1 ); return sum; } }
Problem solution in c++ programming.
class Calculator : public AdvancedArithmetic { int divisorSum(int n) { int sum = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { sum += i; } } return sum; } };
Thanks for the information. Nicely done.
Yess