diff --git a/project_euler/problem_34/sol1.py b/project_euler/problem_34/sol1.py index 126aee9d2..c19fac5de 100644 --- a/project_euler/problem_34/sol1.py +++ b/project_euler/problem_34/sol1.py @@ -4,37 +4,7 @@ Find the sum of all numbers which are equal to the sum of the factorial of their 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 +from math import factorial def sum_of_digit_factorial(n: int) -> int: @@ -45,7 +15,7 @@ def sum_of_digit_factorial(n: int) -> int: >>> sum_of_digit_factorial(0) 1 """ - return sum(factorial(int(digit)) for digit in str(n)) + return sum(factorial(int(char)) for char in str(n)) def compute() -> int: @@ -56,12 +26,9 @@ def compute() -> int: >>> compute() 40730 """ - return sum( - num - for num in range(3, 7 * factorial(9) + 1) - if sum_of_digit_factorial(num) == num - ) + limit = 7 * factorial(9) + 1 + return sum(i for i in range(3, limit) if sum_of_digit_factorial(i) == i) if __name__ == "__main__": - print(compute()) + print(f"{compute()} = ") diff --git a/project_euler/problem_45/__init__.py b/project_euler/problem_45/__init__.py new file mode 100644 index 000000000..792d60054 --- /dev/null +++ b/project_euler/problem_45/__init__.py @@ -0,0 +1 @@ +# diff --git a/project_euler/problem_45/sol1.py b/project_euler/problem_45/sol1.py new file mode 100644 index 000000000..ed66e6fab --- /dev/null +++ b/project_euler/problem_45/sol1.py @@ -0,0 +1,57 @@ +""" +Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: +Triangle T(n) = (n * (n + 1)) / 2 1, 3, 6, 10, 15, ... +Pentagonal P(n) = (n * (3 * n − 1)) / 2 1, 5, 12, 22, 35, ... +Hexagonal H(n) = n * (2 * n − 1) 1, 6, 15, 28, 45, ... +It can be verified that T(285) = P(165) = H(143) = 40755. + +Find the next triangle number that is also pentagonal and hexagonal. +All trinagle numbers are hexagonal numbers. +T(2n-1) = n * (2 * n - 1) = H(n) +So we shall check only for hexagonal numbers which are also pentagonal. +""" + + +def hexagonal_num(n: int) -> int: + """ + Returns nth hexagonal number + >>> hexagonal_num(143) + 40755 + >>> hexagonal_num(21) + 861 + >>> hexagonal_num(10) + 190 + """ + return n * (2 * n - 1) + + +def is_pentagonal(n: int) -> bool: + """ + Returns True if n is pentagonal, False otherwise. + >>> is_pentagonal(330) + True + >>> is_pentagonal(7683) + False + >>> is_pentagonal(2380) + True + """ + root = (1 + 24 * n) ** 0.5 + return ((1 + root) / 6) % 1 == 0 + + +def compute_num(start: int = 144) -> int: + """ + Returns the next number which is traingular, pentagonal and hexagonal. + >>> compute_num(144) + 1533776805 + """ + n = start + num = hexagonal_num(n) + while not is_pentagonal(num): + n += 1 + num = hexagonal_num(n) + return num + + +if __name__ == "__main__": + print(f"{compute_num(144)} = ")