0
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 32: パンデジタル数の積

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

問題 32:パンデジタル数の積

原文 Problem 32: Pandigital products

問題の要約:39×186=7254のように1-9のすべてを使った掛け算になる式の積の合計を求めよ(重複は除く)

いろいろ試してみると右辺の積は4桁、よって左辺は1桁x4桁か2桁x3桁であることが分かります。よって"123456789"の順列すべてを(1,4,4)と(2,3,4)に分割して探します。重複はセットを用いて除きます。

import itertools
ans = set({})
for parr in itertools.permutations("123456789"):
  pstr = ''.join(parr)
  for (la,lb) in [(1,4),(2,3)]:
    a, b, c = int(pstr[:la]),int(pstr[la:la+lb]),int(pstr[la+lb:])
    if a * b == c:
      print(a, b, c)
      ans.add(c)
print(f"Answer = {sum(ans)} ({ans})")

(開発環境:Google Colab)

0
0
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
0
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?