Project Euler with Ruby on WSL [Problem 29]

Level 2 は

50 問だってさ なかなか遠い

問題

Distinct powers

Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

22=4, 23=8, 24=16, 25=32

32=9, 33=27, 34=81, 35=243

42=16, 43=64, 44=256, 45=1024

52=25, 53=125, 54=625, 55=3125

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

私訳

相異なる冪

整数 a, b について ab を考える。 2 ≤ a ≤ 5 かつ 2 ≤ b ≤ 5 の範囲では、 値の異なる項は

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

の 15 ある。(つまり 42 と 24 は値が等しいので一度しか数えない)

2 ≤ a ≤ 100 かつ 2 ≤ b ≤ 100 のとき、相異なる項の数はいくつか?

解答方針

100100 が出現する、という問題はあるものの、 そこは rubyBignum にお任せで。。。

つまり力づく。たかだか 99*99 だし。。。

まぁ正攻法?でやる場合は素因数分解して、

90**3 => "2^3*3^6*5^3"

と一意に変換するような関数を用意して、 これを適当なコレクションに格納するんでしょうなぁ