Skip to content

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 23 commits into from
Aug 13, 2020
Merged
Changes from 1 commit
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
34 changes: 26 additions & 8 deletions project_euler/problem_34/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,33 @@


def factorial(n: int) -> int:
"""
Returns the factorial of n
"""Return the factorial of n.

>>> factorial(5)
120
>>> factorial(1)
1
>>> factorial(0)
1
0
>>> 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
"""

import math

if not n >= 0:
raise ValueError("n must be >= 0")
if math.floor(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:
Expand All @@ -27,7 +45,7 @@ def factorial(n: int) -> int:


def sum_of_digit_factorial(n: int) -> int:
"""
"""
Returns the sum of the digits in n
>>> sum_of_digit_factorial(15)
121
Expand All @@ -41,17 +59,17 @@ def sum_of_digit_factorial(n: int) -> int:

def compute() -> int:
"""
Returns the sum of all numbers whose
Returns the sum of all numbers whose
sum of the factorials of all digits
add up to the number itself.
>>> compute()
40730
"""
summ = 0
the_list = []
for num in range(3, 7 * factorial(9) + 1):
if sum_of_digit_factorial(num) == num:
summ += num
return summ
the_list.append(num)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sir. Will do it

return sum(the_list)


if __name__ == "__main__":
Expand Down