Recursive Digit Sum

  • + 0 comments

    Go solution

    func SuperDigit(digits string, times int) string {
        if len(digits) <= 1 {
            return digits
        }
        return SuperDigit(digitSum(digits, times), 1)
    }
    
    func digitSum(digits string, times int) string {
        sum := 0
        for _, digit := range digits {
            sum += int(digit - '0')
        }
        return strconv.Itoa(sum * times)
    }