Skip to content

created a recursion folder #12141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions recursions/...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Empty file added recursions/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions recursions/recursive_digit_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
The super digit problem is defined as follows:
Given an integer n 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:
- If the number has only one digit, that digit is the super digit.
- Otherwise, the super digit is the super digit of the sum of its digits.

For example, for n = "9875" and k = 4, the concatenated number is:
super_digit(9875987598759875), which can be reduced by summing its digits.
"""

from __future__ import annotations


def super_digit(n_str: str, repetitions: int) -> int:
"""
Computes the super digit of a number formed by concatenating
n_str repetitions times.

Parameters:
n_str (str): The string representation of the integer.
repetitions (int): The number of times to concatenate n_str.

Returns:
int: The super digit of the concatenated number.

>>> super_digit("148", 3)
3
>>> super_digit("9875", 4)
8
>>> super_digit("123", 3)
9
"""

# Calculate the initial sum of the digits in n_str
digit_sum = sum(int(digit) for digit in n_str)

# Multiply the sum by repetitions
total_sum = digit_sum * repetitions

# 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__":
# Read input and split it into n_str and repetitions
first_multiple_input = input().rstrip().split()

n_str = first_multiple_input[0] # n as a string
repetitions = int(first_multiple_input[1]) # repetitions as an integer

# Call the super_digit function and print the result
result = super_digit(n_str, repetitions)
print(result)