Project Euler Problem 44
問題
Pentagonal numbers are generated by the formula, P_(n)=n(3n−1)/2. The first ten pentagonal numbers are: 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... It can be seen that P(4) + P(7) = 22 + 70 = 92 = P(8). However, their difference, 70 − 22 = 48, is not pentagonal. Find the pair of pentagonal numbers, P(j) and P(k), for which their sum and difference is pentagonal and D = |P(k) − P(j)| is minimised; what is the value of D?
ソース
max = 10000 i2p = [0] + (1..max).map{|i| i * (i * 3 - 1) / 2} p2i = {} i2p[1..max].each_index{|i|p2i[i2p[i]] = i} min = i2p.last (1...max).each{|j| (j + 1..max).each{|k| next if p2i[i2p[k] + i2p[j]].nil? next if p2i[i2p[k] - i2p[j]].nil? puts "#{i2p[k]}, #{i2p[j]}" min = [min, i2p[k] - i2p[j]].min } } puts min
解答
5482660
感想
そんなんすぐに現れるだろうと思って、五角数のmaxをもっと少なくしていました。。。
5482660って結構ないんですね(汗