-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path045.py
More file actions
46 lines (37 loc) · 1.19 KB
/
045.py
File metadata and controls
46 lines (37 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# COMPLETED
"""
Problem 45
==========
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(3n−1)/2 1, 5, 12, 22, 35, ...
Hexagonal H[n]=n(2n−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.
Answer: 30dfe3e3b286add9d12e493ca7be63fc
"""
from common import check
PROBLEM_NUMBER = 45
ANSWER_HASH = "30dfe3e3b286add9d12e493ca7be63fc"
tn = 285
pn = 165
hn = 143
triangle = 0
pentagonal = 0
hexagonal = 0
while True:
min_index = min(enumerate([triangle, pentagonal, hexagonal]), key=lambda a: a[1])[0]
if min_index == 0:
tn += 1
triangle = int(tn * (tn + 1) / 2)
elif min_index == 1:
pn += 1
pentagonal = int(pn * (3 * pn - 1) / 2)
elif min_index == 2:
hn += 1
hexagonal = int(hn * (2 * hn - 1))
if triangle == pentagonal == hexagonal:
print(f"T[{tn}] = P[{pn}] = H[{hn}] = {triangle}")
check(triangle, PROBLEM_NUMBER, ANSWER_HASH)
exit()