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 3 years have passed since last update.

【Project Euler】Problem 37: 切り捨て可能素数

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

問題 37:切り捨て可能素数

原文 Problem 37: Truncatable primes

問題の要約:11個しかない左切り捨て可能かつ右切り捨て可能素数の合計を求めよ

切り捨て可能素数(Wikipedia)に説明があります。左右どちらの数字を消していっても素数になる数のことです。上限が書いてありませんが11個しかないということなので11個見つかった時点で終了します。

from sympy import isprime, nextprime
def isTruncPrime(n):
  tl=tr = str(n)
  if len(tl)<=1: return False    # exclude 2,3,5,7
  for i in range(len(str(n))-1):
    tl, tr = tl[1:], tr[:-1]
    if not isprime(int(tl)) or not isprime(int(tr)): return False
  return True

tps, p = [], 7
while len(tps)<11:
  p = nextprime(p)
  if isTruncPrime(p):
    tps.append(p)
print(f"Answer: {sum(tps)}, ({tps})")

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