LoginSignup
1
1

More than 1 year has passed since last update.

【Project Euler】Problem 58: らせんの素数

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

問題 58. らせんの素数

原文 Problem 58: Spiral primes

問題の要約:図のように1から順番にマスにらせん状に書いて行ったとき、対角線の数が素数になる割合が10%未満になるときのマスの辺の長さを求めよ

image.png

Problem 28: 数のらせんの対角線で同様のらせんの対角線の数を求めたので、今回はその数が素数かどうかをチェックします。

from sympy import isprime
n, d = 1, 0
total, pcount = 1, 0
while True:
  d += 2
  for j in range(4):
    n += d
    if isprime(n): pcount +=1
    total+= 1
  if pcount/total < 0.1: break
print(d+1, pcount/total)

(開発環境:Google Colab)

1
1
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
1
1