We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Yes, this solution uses recursion and also does not need the % 9 trick. You can show to yourself that "k" can be multiplied after summing up n by using the problem statement example, n = 9875, and change k from 1 to 5. You'll see that the solutions are multiples of one another, except for when it is 10 (which results in having to sum up the integers)
Also, you can simplify this answer further, so as not to have a "start" variable:
def superDigit(n, k):
sd = sum(int(s) for s in n)
if sd*k < 10:
return sd*k
else:
return superDigit(str(sd*k), 1)
Recursive Digit Sum
You are viewing a single comment's thread. Return to all comments →
python3 solution passed.
Really nice code. Thank you.
Does this really work?
Yes, this solution uses recursion and also does not need the % 9 trick. You can show to yourself that "k" can be multiplied after summing up n by using the problem statement example, n = 9875, and change k from 1 to 5. You'll see that the solutions are multiples of one another, except for when it is 10 (which results in having to sum up the integers)
Also, you can simplify this answer further, so as not to have a "start" variable: