• + 9 comments

    Here is a simple solution written in Python 3. Surprisingly, many other solutions I see don't realize you don't need to iterate though each number - only the first half.

    def divisorSum(self, n):
            if n == 1:
                return 1
            else:
                factor_sum = 1 + n 
                for i in range(2, n//2 + 1):
                    if n % i == 0:
                        factor_sum += i
                return factor_sum