-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheuler045.py
More file actions
executable file
·56 lines (45 loc) · 1.08 KB
/
euler045.py
File metadata and controls
executable file
·56 lines (45 loc) · 1.08 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
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python3
# -*- coding: utf-8 -*-
################################################################################
# Euler 45
# Triangular, pentagonal, and hexagonal
# Author: Eugene Kolo - 2015
# Contact: www.eugenekolo.com
#
# http://projecteuler.net/problem=45
################################################################################
import heapq
def triangle_generator():
n = 1
while True:
t = n * (n + 1) // 2
yield t
n += 1
def pentagonal_generator():
n = 1
while True:
p = n * ((3 * n) - 1) // 2
yield p
n += 1
def hexagonal_generator():
n = 1
while True:
h = n * ((2 * n) - 1)
yield h
n += 1
def solve():
tg = triangle_generator()
pg = pentagonal_generator()
hg = hexagonal_generator()
count = 0
last_n = 0
for n in heapq.merge(tg, pg, hg):
if n == last_n:
count += 1
else:
count = 1
if count == 3 and n > 40755:
return n
last_n = n
if __name__ == '__main__':
print(solve())