0
0

More than 1 year has passed since last update.

【Project Euler】Problem 43: 素数の倍数の部分列

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

問題 43. 素数の倍数の部分列

原文 Problem 43: Sub-string divisibility

問題の要約:10桁のパンデジタル数で$1 \le k \le 7$のとき$k+1,k+2,k+3$の3桁の数が$k$番目の素数で割り切れる数の合計を求めよ。**

やや問題が分かりずらいですがプログラム的にはシンプルです。

from itertools import permutations

primes = [2,3,5,7,11,13,17]
dgts = "0123456789"

def divchk(ps):
  for k in range(7):
    if int(ps[k+1:k+4]) % primes[k] != 0: return False
  return True

print(sum([int(''.join(dp)) for dp in permutations(dgts) if divchk(''.join(dp)) ]))

(開発環境: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