Created With

linkProblem 45 - Triangular, pentagonal, and hexagonal

linkQuestion

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

It can be verified that T285 = P165 = H143 = 40755.

Find the next triangle number that is also pentagonal and hexagonal.

linkSolution

euler_045.py
1linkimport itertools

2link

3linkdef is_triangular(n):

4link if (((8*n) + 1) ** 0.5) % 1 == 0:

5link return True

6link else:

7link return False

8link

9link

10linkdef is_pentagonal(n):

11link if (((((24 * n) + 1) ** 0.5) + 1) / 6) % 1 == 0 :

12link return True

13link else:

14link return False

15link

16link

17linkdef is_hexagonal(n):

18link if (((((8 * n) + 1) ** 0.5) + 1) / 4) % 1 == 0 :

19link return True

20link else:

21link return

22link

23link

24linkdef generate_triangle_number(n):

25link return (n+1) * n * 0.5

26link

27link

28linkskip = [1, 40755]

29linkfor i in itertools.count():

30link triangle = generate_triangle_number(i)

31link if triangle not in skip and is_pentagonal(triangle) and is_hexagonal(triangle):

32link print(triangle)

33link break

linkDiscussion

Problem 45 - Triangular, pentagonal, and hexagonalQuestionSolutionDiscussionAnswer

Home

Project Eulerchevron_right
Essayschevron_right

Awesome Stuff