HOME SUMMARY

Problem 45

1. Problem

Triangular, Pentagonal, and Hexagonal

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

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.

三角形数、五边形数和六边形数

三角形数、五边形数和六边形数分别由以下公式给出:

\[\begin{aligned} &\text{三角形数} && T_n=n(n+1)/2 && 1, 3, 6, 10, 15, \ldots \\ &\text{五边形数} && P_n=n(3n−1)/2 && 1, 5, 12, 22, 35, \ldots \\ &\text{六边形数} && H_n=n(2n−1) && 1, 6, 15, 28, 45, \ldots \end{aligned}\]

可以验证,T285 = P165 = H143 = 40755。

找出下一个同时是三角形数、五边形数和六边形数的数。

2. Solution

当我们把三角形数的 n 变为 2k-1 时,你会发现它成了六边形数,因此我们只需要寻找相等的五边形数和六边形数即可。

假设 Pn = p,那么有:

\[n = \frac{1 + \sqrt{1 + 24p}}{6}\]

找到一个 n 使得 Hn 能够让 \((1+\sqrt{1 + 24H_{n}})/6\) 为整数即可:

(defun eu45-calc (n)
  (let* ((hex (* n (1- (* n 2))))
         (k (/ (+ 1.0 (sqrt (1+ (* hex 24)))) 6)))
    (= k (floor k))))

(cl-loop for i from 150
         if (eu45-calc i)
         return `(,i ,(* i (1- (* i 2)))))
=> (27693 1533776805)