Recursive Digit Sum

  • + 0 comments

    Python solution:

    def superDigit(n: str, k: int) -> int:
        # sum digits of 'n' first and then multiply by k, 
        # instead of summing up digits of int(string * k):
        p = sum(list(map(int, list(n)))) * k
        while p > 10:     # same as len(str(p)) != 1
            p = sum(list(map(int, list(str(p)))))
            # can also use sum(int(digit) for digit in str(p))
        return p