Skip to content

Commit 208b741

Browse files
authored
Update recursive_digit_sum.py
1 parent d368e86 commit 208b741

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

recursions/recursive_digit_sum.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,61 @@
1+
"""
2+
We define super digit of an integer x using the following rules:
13
4+
Given an integer, we need to find the super digit of the integer.
5+
6+
If has only 1 digit, then its super digit is x .
7+
Otherwise, the super digit of x is equal to the super digit of the sum of the digits of .
8+
For example, the super digit 9875 of will be calculated as:
9+
10+
super_digit(9875) 9+8+7+5 = 29
11+
super_digit(29) 2 + 9 = 11
12+
super_digit(11) 1 + 1 = 2
13+
super_digit(2) = 2
14+
15+
16+
ex -2:
17+
Here n=148 and k=3 , so p=148148148 .
18+
19+
super_digit(P) = super_digit(148148148)
20+
= super_digit(1+4+8+1+4+8+1+4+8)
21+
= super_digit(39)
22+
= super_digit(3+9)
23+
= super_digit(12)
24+
= super_digit(1+2)
25+
= super_digit(3)
26+
= 3
27+
"""
28+
29+
30+
"""
31+
32+
Sample Input 0
33+
148 3
34+
35+
Sample Output 0
36+
37+
3
38+
39+
"""
40+
def superDigit(n, k):
41+
# Calculate the initial sum of the digits in n
42+
digit_sum = sum(int(digit) for digit in n)
43+
44+
# Multiply the sum by k
45+
total_sum = digit_sum * k
46+
47+
# Recursive function to find the super digit
48+
while total_sum >= 10:
49+
total_sum = sum(int(digit) for digit in str(total_sum))
50+
51+
return total_sum
52+
53+
if __name__ == '__main__':
54+
first_multiple_input = input().rstrip().split()
55+
56+
n = first_multiple_input[0]
57+
k = int(first_multiple_input[1])
58+
59+
result = superDigit(n, k)
60+
61+
print(result)

0 commit comments

Comments
 (0)