Suppose we have a positive number n, we will add all of its digits to get a new number. Now repeat this operation until it is less than 10.
So, if the input is like 9625, then the output will be 4.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
import math class Solution: def solve(self, n): if n < 10: return n s = 0 l = math.floor(math.log(n, 10) + 1) while l > 0: s += n % 10 n //= 10 l -= 1 return self.solve(s) ob = Solution() print(ob.solve(9625))