이경수 선생님의 수학실험실

Problem 45(Triangular, pentagonal, and hexagonal) 본문

Project Euler

Problem 45(Triangular, pentagonal, and hexagonal)

(이경수) 2019. 8. 15. 19:16

Problem 45(Triangular, pentagonal, and hexagonal)

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle \(T_{n}=\frac{n(n+1)}{2}\) 1, 3, 6, 10, 15, ...
Pentagonal \(P_{n}=\frac{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.

 

In Python:

import time
import math

def ispentagon(n):
    if math.sqrt(1 + 24 * n) % 6 == 5:
        return True

def ishexagon(n):
    if math.sqrt(1 + 8 * n) %  4 == 3:
        return True

startTime = time.time()
i = 286
while True:
    trialge = i * (i + 1) // 2
    if ispentagon(trialge) and ishexagon(trialge):
        break
    else:
        i += 1
print(trialge)
print(time.time() - startTime, "seconds")

Run time: 0.05330491065979004 seconds

 

Solution: 1533776805

'Project Euler' 카테고리의 다른 글

Problem 47(Distinct primes factors)  (0) 2019.08.15
Problem 46(Goldbach's other conjecture)  (0) 2019.08.15
Problem 44(Pentagon numbers)  (0) 2019.07.24
Problem 43(Sub-string divisibility)  (0) 2019.07.24
Problem 42(Coded triangle numbers)  (0) 2019.07.24
Comments