Repeated sum of digits
Given an integer N, recursively sum digits of N until we get a single digit. The process can be described below
If N < 10
digSum(N) = N
Else
digSum(N) = Sum(digSum(N))
Example 1:
Input: N = 1234 Output: 1 Explanation: The sum of 1+2+3+4 = 10, digSum(x) == 10 Hence ans will be 1+0 = 1
Example 2:
Input: N = 9999 Output: 9 Explanation: Check it yourself.
Your Task:
You don't need to read input or print anything. Your task is to complete the function repeatedSumOfDigits() which takes an integer N and returns the repeated sum of digits of N.
class Solution:
def repeatedSumOfDigits (self,N):
self.N=N
s=0
for i in str(N):
s+=int(i)
#return s
if s<10:
return s
else:
s1=0
for i in str(s):
s1+=int(i)
if s1<10:
return s1
else:
s2=0
for i in str(s1):
s2+=int(i)
return s2
Comments
Post a Comment