-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
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
sharath4444
wants to merge
16
commits into
TheAlgorithms:master
Choose a base branch
from
sharath4444:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
created a recursion folder #12141
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b45d260
Create recursions
sharath4444 bf42e68
Delete recursions
sharath4444 65dbfec
Create ...
sharath4444 d368e86
Create recursive_digit_sum.py
sharath4444 208b741
Update recursive_digit_sum.py
sharath4444 1d034b7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3cf6be0
Update recursive_digit_sum.py
sharath4444 4cde2d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 849e0b3
Update recursive_digit_sum.py
sharath4444 545d4aa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cbd20bf
Update recursive_digit_sum.py
sharath4444 7b27f18
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0d8e09c
Create __init__.py
sharath4444 fc8f143
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d412a6e
Update recursive_digit_sum.py
sharath4444 fe495e2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
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 | ||
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 superDigit(n: str, k: int) -> int: | ||
Check failure on line 17 in recursions/recursive_digit_sum.py
|
||
""" | ||
Computes the super digit of a number formed by concatenating n k times. | ||
|
||
Parameters: | ||
n (str): The string representation of the integer. | ||
k (int): The number of times to concatenate n. | ||
|
||
Returns: | ||
int: The super digit of the concatenated number. | ||
|
||
>>> superDigit("148", 3) | ||
3 | ||
>>> superDigit("9875", 4) | ||
8 | ||
>>> superDigit("123", 3) | ||
9 | ||
""" | ||
|
||
# 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__': | ||
# 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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.