Recursive Digit Sum

  • + 0 comments

    Python solution. Another terrible formulated problem, look at the line:

    n = str(sum(int(digit) for digit in n))k

    This is not what the problem asks, but works, because when working with big numbers you cannot simply multiply the string, here throws a Memory Error, you need to reduce first, something you wouldn't do if was a normal work, you NEED the exception that was throw to investigate, that is how a developer work, ffs!

    Also who knows the math that multiply the sum of digits is the same of sum the multiply of the original number?

    Where are those genius?

    def superDigit(n, k):
        # Apply k at the beginning
        n = str(sum(int(digit) for digit in n))*k
        # Reduce until single digit
        while len(n) > 1:
            n = str(sum(int(digit) for digit in n))
        return int(n)