Skip to content

Commit 402ba7f

Browse files
Kush1101cclauss
andauthored
Created problem_45 in project_euler and Speed Boost for problem_34/sol1.py (TheAlgorithms#2349)
* Create __init__.py * Add files via upload * Update sol1.py * Update sol1.py * Update project_euler/problem_45/sol1.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update sol1.py * Update sol1.py * Update project_euler/problem_34/sol1.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update project_euler/problem_34/sol1.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update sol1.py * Update project_euler/problem_34/sol1.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update sol1.py * Update project_euler/problem_34/sol1.py Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 5cfc017 commit 402ba7f

File tree

3 files changed

+63
-38
lines changed

3 files changed

+63
-38
lines changed

project_euler/problem_34/sol1.py

+5-38
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,7 @@
44
Note: As 1! = 1 and 2! = 2 are not sums they are not included.
55
"""
66

7-
8-
def factorial(n: int) -> int:
9-
"""Return the factorial of n.
10-
>>> factorial(5)
11-
120
12-
>>> factorial(1)
13-
1
14-
>>> factorial(0)
15-
1
16-
>>> factorial(-1)
17-
Traceback (most recent call last):
18-
...
19-
ValueError: n must be >= 0
20-
>>> factorial(1.1)
21-
Traceback (most recent call last):
22-
...
23-
ValueError: n must be exact integer
24-
"""
25-
26-
if not n >= 0:
27-
raise ValueError("n must be >= 0")
28-
if int(n) != n:
29-
raise ValueError("n must be exact integer")
30-
if n + 1 == n: # catch a value like 1e300
31-
raise OverflowError("n too large")
32-
result = 1
33-
factor = 2
34-
while factor <= n:
35-
result *= factor
36-
factor += 1
37-
return result
7+
from math import factorial
388

399

4010
def sum_of_digit_factorial(n: int) -> int:
@@ -45,7 +15,7 @@ def sum_of_digit_factorial(n: int) -> int:
4515
>>> sum_of_digit_factorial(0)
4616
1
4717
"""
48-
return sum(factorial(int(digit)) for digit in str(n))
18+
return sum(factorial(int(char)) for char in str(n))
4919

5020

5121
def compute() -> int:
@@ -56,12 +26,9 @@ def compute() -> int:
5626
>>> compute()
5727
40730
5828
"""
59-
return sum(
60-
num
61-
for num in range(3, 7 * factorial(9) + 1)
62-
if sum_of_digit_factorial(num) == num
63-
)
29+
limit = 7 * factorial(9) + 1
30+
return sum(i for i in range(3, limit) if sum_of_digit_factorial(i) == i)
6431

6532

6633
if __name__ == "__main__":
67-
print(compute())
34+
print(f"{compute()} = ")

project_euler/problem_45/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#

project_euler/problem_45/sol1.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
3+
Triangle T(n) = (n * (n + 1)) / 2 1, 3, 6, 10, 15, ...
4+
Pentagonal P(n) = (n * (3 * n − 1)) / 2 1, 5, 12, 22, 35, ...
5+
Hexagonal H(n) = n * (2 * n − 1) 1, 6, 15, 28, 45, ...
6+
It can be verified that T(285) = P(165) = H(143) = 40755.
7+
8+
Find the next triangle number that is also pentagonal and hexagonal.
9+
All trinagle numbers are hexagonal numbers.
10+
T(2n-1) = n * (2 * n - 1) = H(n)
11+
So we shall check only for hexagonal numbers which are also pentagonal.
12+
"""
13+
14+
15+
def hexagonal_num(n: int) -> int:
16+
"""
17+
Returns nth hexagonal number
18+
>>> hexagonal_num(143)
19+
40755
20+
>>> hexagonal_num(21)
21+
861
22+
>>> hexagonal_num(10)
23+
190
24+
"""
25+
return n * (2 * n - 1)
26+
27+
28+
def is_pentagonal(n: int) -> bool:
29+
"""
30+
Returns True if n is pentagonal, False otherwise.
31+
>>> is_pentagonal(330)
32+
True
33+
>>> is_pentagonal(7683)
34+
False
35+
>>> is_pentagonal(2380)
36+
True
37+
"""
38+
root = (1 + 24 * n) ** 0.5
39+
return ((1 + root) / 6) % 1 == 0
40+
41+
42+
def compute_num(start: int = 144) -> int:
43+
"""
44+
Returns the next number which is traingular, pentagonal and hexagonal.
45+
>>> compute_num(144)
46+
1533776805
47+
"""
48+
n = start
49+
num = hexagonal_num(n)
50+
while not is_pentagonal(num):
51+
n += 1
52+
num = hexagonal_num(n)
53+
return num
54+
55+
56+
if __name__ == "__main__":
57+
print(f"{compute_num(144)} = ")

0 commit comments

Comments
 (0)