Recursive Digit Sum

  • + 1 comment

    Python solution

    Explaining the code:

    "@nemat-al/recursive-digit-sum-hackerrank-python-db446d084a89">https://medium.com/@nemat-al/recursive-digit-sum-hackerrank-python-db446d084a89"

    def superDigit(n, k):
       # Base case: If n is already a single-digit number, return it as the super digit
       if len(n) == 1:
          return int(n)
       # Calculate the sum of the digits of n multiplied by k
       added = sum(int(i) for i in n) * k
       # Recursive call: with the sum converted to string and k set to 1
       # since repeated multiplication is only needed once  
       return superDigit(str(added), 1)