Sort by

recency

|

811 Discussions

|

  • + 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)

  • + 1 comment
    class Calculator(AdvancedArithmetic):
        def divisorSum(self, n: int) -> int:
            if n < 2:
                return n
            divisors = [1, n]
            divisors.extend(filter(lambda x: n % x == 0, range(2, int(n / 2) + 1)))
            return sum(divisors)
    
  • + 0 comments

    Python3 :

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