1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Project Euler】Problem 90: 2つの立方体の数字

Last updated at Posted at 2022-02-25
  • 本記事はProjectEulerの「100番以下の問題の説明は記載可能」という規定に基づいて回答のヒントが書かれていますので、自分である程度考えてみてから読まれることをお勧めします。

問題 90.2つの立方体の数字

原文 Problem 90: Cube digit pairs

問題の要約:立方体の面に0-9の異なる数字を書いて2つ並べたときに以下のように平方数になる書き方を数えよ。ただし9と6は逆さに置いてどちらにも使える。

image.png

以下のステップで全探索で解きました。

  1. 0-9から6個の数字を選ぶ組み合わせを2つ作り、その直積を取る
  2. それに6か9が含まれていたら、6と9を加えて、その2つの立方体の2つから出来る2桁の数字の集合(2つの順序を入れ替えたものも含めて)を作る。
  3. それに2桁の平方数が全部含まれていれば関数isSquareはTrueを返すので、それを数えて2で割る。
from itertools import chain
from itertools import combinations as comb
from itertools import product as prod
sqn = set(["01","04","09","16","25","36","49","64","81"])
dgt = [str(i) for i in range(10)]

def a69(lst):   # add 6 and 9 if lst includes 6 or 9
  return set(lst) | {"6","9"} if len(set(lst) & {"6","9"}) > 0 else set(lst)

def isSquare(cub, d1, d2):
  return  cub <= set(chain.from_iterable([((a+b),(b+a)) for a,b in prod(a69(d1), a69(d2))]))

# divide 2 considering the duplication
print(f"Answer : {[isSquare(sqn,d1,d2) for d1,d2 in prod(comb(dgt,6),comb(dgt,6))].count(True)//2}")

開発環境:Google Colab

1
0
1

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?