• + 2 comments

    **Python 3: **

    def best_divisor(n):
        sums = []
        divisors = []
        for i in range(1, n + 1):
            if n % i == 0:
                divisors.append(i)
                sums.append(sum_digits(i))
    
        # index = sums.index(max(sums))
        # print(divisors[index])
        return divisors[sums.index(max(sums))]
    
    def sum_digits(n):
        sum = 0
        for d in str(n):
            sum += int(d)
    
        return sum