LoginSignup
0
0

More than 1 year has passed since last update.

【Project Euler】Problem 41: パンデジタル素数

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

問題 41. パンデジタル素数

原文 Problem 41: Pandigital prime

問題の要約:n桁のパンデジタル数で素数のものの最大値を求めよ

パンデジタル数はもう何度も出てきていますが、今回は素数。例として素数2143が4桁パンデジタル数として挙げられています。これも最大値を求めよなので大きい順に探していきます。

import itertools
import sympy

digits9 = "123456789"
found = False
for n in range(9,0,-1):     
  digits = digits9[n-1::-1]  # "987654321", "87654321",,,"21","1"
  for pstr in itertools.permutations(digits):  # pandigital number in decending order
    ps = ''.join(pstr)
    if sympy.isprime(int(ps)):
      found = True
      break
  if found: break
print(f"Answer: {ps}")

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