-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Hacktoberfest: Add solution to problem 74 #3110
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d5bfe66
Add solution to problem 74
GGn0 bafe7b4
Fix typo
GGn0 e59d8cf
Edit unnecessary comment
GGn0 4722bff
Rename folder, add default params in solution()
GGn0 faf6dc3
Rename file to solve conflicts
GGn0 b3bce98
Fix doctests
GGn0 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
Empty file.
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,114 @@ | ||
""" projectEuler problem 74 | ||
Starting from any positive integer number | ||
it is possible to attain another one summing the factorial of its digits. | ||
|
||
Repeating this step, we can build chains of numbers. | ||
It is not difficult to prove that EVERY starting number | ||
will eventually get stuck in a loop. | ||
|
||
The request is to find how many numbers less than one million | ||
produce a chain with exactly 60 non repeating items. | ||
|
||
Approach: | ||
This solution simply consists in a loop that generates | ||
the chains of non repeating items. | ||
The generation of the chain stops before a repeating item | ||
or if the size of the chain is greater then the desired one. | ||
After generating each chain, the length is checked and the counter increases. | ||
""" | ||
|
||
# The desired number of items to check for in the non repeating chains | ||
EXACT_CHAIN_LENGTH = 60 | ||
|
||
# The maximum number to check in the solution | ||
MAX_NUMBER = 1000000 | ||
|
||
|
||
def factorial(a: int) -> int: | ||
"""Returns the factorial of the input a | ||
>>> factorial(5) | ||
120 | ||
|
||
>>> factorial(6) | ||
720 | ||
|
||
>>> factorial(0) | ||
1 | ||
""" | ||
|
||
# The factorial function is not defined for negative numbers | ||
if a < 0: | ||
raise ValueError("Invalid negative input!", a) | ||
|
||
# The case of 0! is handled separately | ||
if a == 0: | ||
return 1 | ||
else: | ||
# use a temporary support variable to store the computation | ||
temporary_computation = 1 | ||
|
||
while a > 0: | ||
temporary_computation *= a | ||
a -= 1 | ||
|
||
return temporary_computation | ||
|
||
|
||
def factorial_sum(a: int) -> int: | ||
"""Function to perform the sum of the factorial | ||
of all the digits in a | ||
|
||
>>> factorial_sum(69) | ||
363600 | ||
""" | ||
|
||
# Prepare a variable to hold the computation | ||
fact_sum = 0 | ||
|
||
""" Convert a in string to iterate on its digits | ||
convert the digit back into an int | ||
and add its factorial to fact_sum. | ||
""" | ||
for i in str(a): | ||
fact_sum += factorial(int(i)) | ||
|
||
return fact_sum | ||
|
||
|
||
def solution() -> int: | ||
"""Returns the number of numbers that produce | ||
chains with exactly 60 non repeating elements. | ||
>>> solution() | ||
402 | ||
""" | ||
|
||
# the counter for the chains with the exact desired length | ||
chain_counter = 0 | ||
|
||
for i in range(1, MAX_NUMBER + 1): | ||
|
||
# The temporary list will contain the elements of the chain | ||
chain_list = [i] | ||
|
||
# The new element of the chain | ||
new_chain_element = factorial_sum(chain_list[-1]) | ||
|
||
""" Stop computing the chain when you find a repeating item | ||
or the length it greater then the desired one. | ||
""" | ||
while not (new_chain_element in chain_list) and ( | ||
len(chain_list) <= EXACT_CHAIN_LENGTH | ||
): | ||
chain_list += [new_chain_element] | ||
|
||
new_chain_element = factorial_sum(chain_list[-1]) | ||
|
||
""" If the while exited because the chain list contains the exact amount of elements | ||
increase the counter | ||
""" | ||
chain_counter += len(chain_list) == EXACT_CHAIN_LENGTH | ||
|
||
return chain_counter | ||
|
||
|
||
print(solution()) | ||
GGn0 marked this conversation as resolved.
Show resolved
Hide resolved
|
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.