Pentagonal numbers are generated by the formula, . The first ten
pentagonal numbers are:
It can be seen that . However, their difference, , is not pentagonal.
Find the pair of pentagonal numbers, and , for which their sum and difference
are pentagonal and
is minimised; what is the value of ?
解决方案
先从小到大处理五边形数和值,再从小到大处理五边形数差值。
通过一个二元一次方程 解出 和 的值,如果 和 也都是五边形数,那么可以输出所需值。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
from itertools import count
# a+b=s,a-b=d,a-d=b defsolve(): p = [] st = set() for s in (n * (3 * n - 1) // 2for n in count(1)): st.add(s) for d in p: if ((d ^ s) & 1) == 0: a = (d + s) >> 1 if a in st and s - a in st: return d p.append(s)