LoginSignup
1
1

More than 5 years have passed since last update.

Project Euler 62「立方数置換」

Posted at

このくらいの難易度が解いてて気持ちいいですね。

Problem 62 「立方数置換」

立方数 41063625 (345^3) は, 桁の順番を入れ替えると2つの立方数になる: 56623104 (384^3) と 66430125 (405^3) である. 41063625は, 立方数になるような桁の置換をちょうど3つもつ最小の立方数である.
立方数になるような桁の置換をちょうど5つもつ最小の立方数を求めよ.

def hoge(num):
    n, m, d = 1, 0, {}
    while 1:
        n3 = n**3
        if m != 0 and len(str(m)) < len(str(n3)):
            return m
        s = ''.join(sorted(str(n3)))
        d[s] = d.get(s, []) + [n3]
        if len(d[s]) == num:
            #print(d[s])
            m = d[s][0] if m == 0 else min(m, d[s][0])
        n += 1

print(hoge(5))
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1