mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-31 06:33:44 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
3cf6be0b7b
commit
4cde2d498b
|
@ -1,7 +1,7 @@
|
|||
"""
|
||||
The super digit problem is defined as follows:
|
||||
Given an integer represented as a string and an integer k,
|
||||
the goal is to find the super digit of the number formed by concatenating
|
||||
Given an integer represented as a string and an integer k,
|
||||
the goal is to find the super digit of the number formed by concatenating
|
||||
the integer n k times.
|
||||
|
||||
The super digit of a number is defined recursively:
|
||||
|
@ -14,6 +14,7 @@ super_digit(9875987598759875), which can be reduced by summing its digits.
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def superDigit(n: str, k: int) -> int:
|
||||
"""
|
||||
Computes the super digit of a number formed by concatenating n k times.
|
||||
|
@ -35,23 +36,24 @@ def superDigit(n: str, k: int) -> int:
|
|||
|
||||
# Calculate the initial sum of the digits in n
|
||||
digit_sum = sum(int(digit) for digit in n)
|
||||
|
||||
|
||||
# Multiply the sum by k
|
||||
total_sum = digit_sum * k
|
||||
|
||||
|
||||
# Recursive function to find the super digit
|
||||
while total_sum >= 10:
|
||||
total_sum = sum(int(digit) for digit in str(total_sum))
|
||||
|
||||
|
||||
return total_sum
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Read input and split it into n and k
|
||||
first_multiple_input = input().rstrip().split()
|
||||
|
||||
|
||||
n = first_multiple_input[0] # n as a string
|
||||
k = int(first_multiple_input[1]) # k as an integer
|
||||
|
||||
|
||||
# Call the superDigit function and print the result
|
||||
result = superDigit(n, k)
|
||||
print(result)
|
||||
|
|
Loading…
Reference in New Issue
Block a user