-
Notifications
You must be signed in to change notification settings - Fork 0
/
P045.py
41 lines (29 loc) · 814 Bytes
/
P045.py
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
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 2 10:20:17 2020
@author: zhixia liu
"""
"""
Project Euler 45: Triangular, pentagonal, and hexagonal
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ...
Pentagonal Pn=n(3n−1)/2 1, 5, 12, 22, 35, ...
Hexagonal Hn=n(2n−1) 1, 6, 15, 28, 45, ...
It can be verified that T285 = P165 = H143 = 40755.
Find the next triangle number that is also pentagonal and hexagonal.
"""
#%%
from math import sqrt
def isPentagonal(m):
t = (1+sqrt(1+24*m))/6
return t.is_integer()
def isTriangle(m):
t = (-1+sqrt(1+8*m))/2
return t.is_integer()
n=144
while True:
h = n*(2*n-1)
if isTriangle((h)) and isPentagonal(h):
print(h)
break
n += 1