Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@yopya

Project Euler 62「立方数置換」

More than 3 years have passed since last update.

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

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
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
yopya
自然に囲まれた田舎で働きたい。 田舎でPythonの仕事ないっすか?

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?