Sudal's Garage

Project Euler 45 본문

Programming/Project Euler - python

Project Euler 45

_Sudal 2019. 4. 7. 05:00

Question: 


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.


문제:


삼각수, 오각수, 육각수는 다음과 같은 공식들로 만들어진다.


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, ...


우리는 T285 = P165 = H143 = 40755 임을 확인 할 수 있다.

40755 다음의 삼각수이면서 오각수이면서 육각수 인 수는 얼마인가?


Solution:


from math import sqrt

def isTri(n):
    x = (sqrt(8*n + 1) - 1) / 2
    return x == int(x)
 
def isPenta(n):
    x = (sqrt(24*n + 1) + 1) / 6
    return x == int(x)

def isHexa(n):
    x = (sqrt(8*n + 1) + 1) / 4
    return x == int(x)

x = 286

while True:
    n = int((x * (x+1)) / 2)
    if isPenta(n) and isHexa(n):
        break
    x += 1

print('{}'.format(n))


삼각수 판별은 다음 글을 확인하자.

2019/04/03 - [Programming/Project Euler - python] - Project Euler 42

오각수 판별은 다음 글을 확인하자.

2019/04/05 - [Programming/Project Euler - python] - Project Euler 44


다음은 육각수 판별법이다. 출처: https://en.wikipedia.org/wiki/Hexagonal_number




'Programming > Project Euler - python' 카테고리의 다른 글

Project Euler 44  (0) 2019.04.06
Project Euler 43  (0) 2019.04.05
Project Euler 42  (0) 2019.04.04
Project Euler 41  (0) 2019.04.03
Project Euler 40  (0) 2019.04.02