Triangular, pentagonal, and hexagonal


Fork me on GitHub
2014-12-11

Problem 045: Triangular, pentagonal, and hexagonal

Description:

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

Name Formula Numbers
Triangle T_{n} = n(n+1)/2 1, 3, 6, 10, 15, ...
Pentagonal P_{n} = 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 T285 = P165 = H143 = 40755.

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


Solution:
v                 >v
>8::**::**8*:*20pv0v>vv p03+g03*2:p01-\g01+g03:<
v           +"l#"<3pp2# v/4    <   >:30g+10g`! |
>1+::2*1-*64**v   010>0g>:10g`#^_>:|:/4p03/2g03<
^        <  $<>1+:^>^ >30g2/30p4/^ >$30g:*-v
@.*-1*2:_^#!_^#!                    -5%6g03<
Start
??
Pause
Reset
Output:
Stack:   (0)

Explanation:

This problem is very similar to the previous one. We iterate through all Pentagonal numbers (starting at P_144) and test the numbers if they are hexagonal.

The test for hexagonal numbers is the same as in Problem-44, but we have to expand the iSquare function for int64 numbers ([20] is now 2^60 instead of 2^30).

The major trick is that we only need to test for the hexagonal property. Because all hexagonal numbers are also Triangle numbers. Think about it, a Hexagon has six edges and a Triangle three, so every Hexagonal contains two triangles.

H_{n} == T_{2*n}


Interpreter steps: 25 312 679
Execution time (BefunExec): 3.49s (7.24 MHz)
Program size: 48 x 6 (fully conform befunge-93)
Solution: 1533776805
Solved at: 2014-12-11



made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript