Skip to content

Commit 3cf6be0

Browse files
authored
Update recursive_digit_sum.py
1 parent 1d034b7 commit 3cf6be0

File tree

1 file changed

+35
-41
lines changed

1 file changed

+35
-41
lines changed

recursions/recursive_digit_sum.py

+35-41
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,57 @@
11
"""
2-
We define super digit of an integer x using the following rules:
2+
The super digit problem is defined as follows:
3+
Given an integer represented as a string and an integer k,
4+
the goal is to find the super digit of the number formed by concatenating
5+
the integer n k times.
36
4-
Given an integer, we need to find the super digit of the integer.
7+
The super digit of a number is defined recursively:
8+
- If the number has only one digit, that digit is the super digit.
9+
- Otherwise, the super digit is the super digit of the sum of its digits.
510
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
11+
For example, for n = "9875" and k = 4, the concatenated number is:
12+
super_digit(9875987598759875), which can be reduced by summing its digits.
2713
"""
2814

29-
"""
30-
31-
Sample Input 0
32-
148 3
15+
from __future__ import annotations
3316

34-
Sample Output 0
17+
def superDigit(n: str, k: int) -> int:
18+
"""
19+
Computes the super digit of a number formed by concatenating n k times.
3520
36-
3
21+
Parameters:
22+
n (str): The string representation of the integer.
23+
k (int): The number of times to concatenate n.
3724
38-
"""
25+
Returns:
26+
int: The super digit of the concatenated number.
3927
28+
>>> superDigit("148", 3)
29+
3
30+
>>> superDigit("9875", 4)
31+
8
32+
>>> superDigit("123", 3)
33+
9
34+
"""
4035

41-
def superDigit(n, k):
4236
# Calculate the initial sum of the digits in n
4337
digit_sum = sum(int(digit) for digit in n)
44-
38+
4539
# Multiply the sum by k
4640
total_sum = digit_sum * k
47-
41+
4842
# Recursive function to find the super digit
4943
while total_sum >= 10:
5044
total_sum = sum(int(digit) for digit in str(total_sum))
51-
45+
5246
return total_sum
5347

54-
55-
if __name__ == "__main__":
48+
if __name__ == '__main__':
49+
# Read input and split it into n and k
5650
first_multiple_input = input().rstrip().split()
57-
58-
n = first_multiple_input[0]
59-
k = int(first_multiple_input[1])
60-
51+
52+
n = first_multiple_input[0] # n as a string
53+
k = int(first_multiple_input[1]) # k as an integer
54+
55+
# Call the superDigit function and print the result
6156
result = superDigit(n, k)
62-
6357
print(result)

0 commit comments

Comments
 (0)