Sort by

recency

|

813 Discussions

|

  • + 0 comments

    TypeScript

    interface AdvancedArithmetic {
        divisorSum(n: number): number;
    }
    
    class Calculator implements AdvancedArithmetic {
        divisorSum(n: number): number {
            let sum = 0;
            for (let i = 1; i <= n; i++) {
                if (n % i === 0) {
                    sum += i;
                }
            }
            return sum;
        }
    }
    
    function main() {
       let n: number = parseInt(readLine(), 10);
       const myCalculator: AdvancedArithmetic = new Calculator(); 
       const sum = myCalculator.divisorSum(n);
    
        const interfaceName = "AdvancedArithmetic";
       console.log("I implemented: " + interfaceName);
       console.log(sum);
    }
    
  • + 0 comments
    class Calculator(AdvancedArithmetic):
        def divisorSum(self, n):
            return sum([x for x in range(1,n+1) if n%x==0])
    
  • + 0 comments

    Python 3 - Code to Optimizing time

    class Calculator(AdvancedArithmetic):
        def divisorSum(self, n):
            divisors = set()            # Divisors are unique so using set
            for i in range(1,int(n**(1/2))+1):    # Going upto sqrt of n
                if n % i == 0:
                    divisors.add(i)        # Adding divisor to divisors set
                    divisors.add(n/i)    # Adding Quotient to divisors set
            return int(sum(divisors))
    
  • + 0 comments

    `In Java 8 import java.io.; import java.util.;

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

    class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        scan.close();
    
        AdvancedArithmetic myCalculator = new Calculator(); 
        int sum = myCalculator.divisorSum(n);
        System.out.println("I implemented: " + myCalculator.getClass().getInterfaces()[0].getName() );
        System.out.println(sum);
    }
    

    } `

  • + 0 comments

    def divisorSum(self, n): divisors = [] for i in range(1,n+1): if n%i==0: divisors.append(i) return sum(divisors)