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 49: 並び替え素数

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

問題 49. 並び替え素数

原文 Problem 49: Prime permutations

問題の要約:4桁の素数を並べ替えた2つの数も素数で、かつ3つの数が等差数列になっているとき、その順番に連結した12桁の数を求めよ

関数isPermSameで2つの数が並び替えで同じになるかチェックしています。

from sympy import nextprime, isprime
def isPermSame(n1, n2):
  return ("".join(sorted(str(n1)))) == ("".join(sorted(str(n2))))

p, ans = nextprime(1000), 0
while p < 10000:
  for inc in range(2,5000):
    p1, p2 = p+inc, p+inc+inc
    if p2 < 10000 and p != 1487 and \
       isprime(p1) and isprime(p2) and \
       isPermSame(p, p1) and isPermSame(p, p2):
      ans = "".join([str(p),str(p+inc), str(p+inc+inc)])
  p = nextprime(p)

print(f"Answer: {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?