Skip to content

Commit 849e0b3

Browse files
authored
Update recursive_digit_sum.py
1 parent 4cde2d4 commit 849e0b3

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

recursions/recursive_digit_sum.py

+24-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
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
3+
Given an integer n represented as a string and an integer k,
4+
the goal is to find the super digit of the number formed by concatenating
55
the integer n k times.
66
77
The super digit of a number is defined recursively:
@@ -14,46 +14,44 @@
1414

1515
from __future__ import annotations
1616

17-
18-
def superDigit(n: str, k: int) -> int:
17+
def super_digit(n_str: str, repetitions: int) -> int:
1918
"""
20-
Computes the super digit of a number formed by concatenating n k times.
19+
Computes the super digit of a number formed by concatenating n_str repetitions times.
2120
2221
Parameters:
23-
n (str): The string representation of the integer.
24-
k (int): The number of times to concatenate n.
22+
n_str (str): The string representation of the integer.
23+
repetitions (int): The number of times to concatenate n_str.
2524
2625
Returns:
2726
int: The super digit of the concatenated number.
2827
29-
>>> superDigit("148", 3)
28+
>>> super_digit("148", 3)
3029
3
31-
>>> superDigit("9875", 4)
30+
>>> super_digit("9875", 4)
3231
8
33-
>>> superDigit("123", 3)
32+
>>> super_digit("123", 3)
3433
9
3534
"""
3635

37-
# Calculate the initial sum of the digits in n
38-
digit_sum = sum(int(digit) for digit in n)
39-
40-
# Multiply the sum by k
41-
total_sum = digit_sum * k
42-
36+
# Calculate the initial sum of the digits in n_str
37+
digit_sum = sum(int(digit) for digit in n_str)
38+
39+
# Multiply the sum by repetitions
40+
total_sum = digit_sum * repetitions
41+
4342
# Recursive function to find the super digit
4443
while total_sum >= 10:
4544
total_sum = sum(int(digit) for digit in str(total_sum))
46-
45+
4746
return total_sum
4847

49-
50-
if __name__ == "__main__":
51-
# Read input and split it into n and k
48+
if __name__ == '__main__':
49+
# Read input and split it into n_str and repetitions
5250
first_multiple_input = input().rstrip().split()
53-
54-
n = first_multiple_input[0] # n as a string
55-
k = int(first_multiple_input[1]) # k as an integer
56-
57-
# Call the superDigit function and print the result
58-
result = superDigit(n, k)
51+
52+
n_str = first_multiple_input[0] # n as a string
53+
repetitions = int(first_multiple_input[1]) # repetitions as an integer
54+
55+
# Call the super_digit function and print the result
56+
result = super_digit(n_str, repetitions)
5957
print(result)

0 commit comments

Comments
 (0)