-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Created problem_34 in project_euler #2305
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 all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
dcdaa1e
Create __init__.py
Kush1101 16f0943
Add files via upload
Kush1101 f7d92f8
Rename solution.py.py to solution.py
Kush1101 a915dea
Delete __init__.py
Kush1101 c47288c
Update and rename solution.py to sol1.py
Kush1101 632b838
Update sol1.py
Kush1101 f801415
Create __init__.py
Kush1101 39bdd95
Update __init__.py
Kush1101 2cf433f
Delete __init__.py
Kush1101 0e7d56b
Add files via upload
Kush1101 b5a56bb
Update __init__.py
Kush1101 fd043c0
Add #\n
cclauss e551e5a
Update sol1.py
Kush1101 5e92188
Update sol1.py
Kush1101 0cf3459
Update sol1.py
Kush1101 07e43df
Update project_euler/problem_34/sol1.py
Kush1101 5d76c7e
Update project_euler/problem_34/sol1.py
Kush1101 b5e771f
Update project_euler/problem_34/sol1.py
Kush1101 8f0b7c1
Update project_euler/problem_34/sol1.py
Kush1101 bd0849a
Update sol1.py
Kush1101 53cf0a5
Update sol1.py
Kush1101 b038727
Update sol1.py
Kush1101 4c38019
Use int(n) instead of floor(n)
cclauss 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,67 @@ | ||
""" | ||
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. | ||
Find the sum of all numbers which are equal to the sum of the factorial of their digits. | ||
Note: As 1! = 1 and 2! = 2 are not sums they are not included. | ||
""" | ||
|
||
|
||
def factorial(n: int) -> int: | ||
"""Return the factorial of n. | ||
>>> factorial(5) | ||
120 | ||
>>> factorial(1) | ||
1 | ||
>>> factorial(0) | ||
1 | ||
>>> factorial(-1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: n must be >= 0 | ||
>>> factorial(1.1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: n must be exact integer | ||
""" | ||
|
||
if not n >= 0: | ||
raise ValueError("n must be >= 0") | ||
if int(n) != n: | ||
raise ValueError("n must be exact integer") | ||
if n + 1 == n: # catch a value like 1e300 | ||
raise OverflowError("n too large") | ||
result = 1 | ||
factor = 2 | ||
while factor <= n: | ||
result *= factor | ||
factor += 1 | ||
return result | ||
|
||
|
||
def sum_of_digit_factorial(n: int) -> int: | ||
""" | ||
Returns the sum of the digits in n | ||
>>> sum_of_digit_factorial(15) | ||
121 | ||
>>> sum_of_digit_factorial(0) | ||
1 | ||
""" | ||
return sum(factorial(int(digit)) for digit in str(n)) | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def compute() -> int: | ||
""" | ||
Returns the sum of all numbers whose | ||
sum of the factorials of all digits | ||
add up to the number itself. | ||
>>> compute() | ||
40730 | ||
""" | ||
return sum( | ||
num | ||
for num in range(3, 7 * factorial(9) + 1) | ||
if sum_of_digit_factorial(num) == num | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
print(compute()) |
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.