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 7: 10001番目の素数

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

問題 7: 10001番目の素数

原文 Problem 7: 10001st prime

まずは自力で奇数を順番に生成した素数で割って行って求めてみます。実行時間約16秒。

import itertools
pn=[2]
for n in itertools.count(3,2):
    for p in pn:
      divd = (n%p == 0)
      if divd: break
    if not divd: pn.append(n)
    if len(pn) >= 10001: break
print(f"Answer : {pn[-1]}")

安易ですが、前にも使ったsympyのnextprimeは,何番目か指定できるので一発で瞬時に求まりました。

from sympy import nextprime
print(f"Answer : {nextprime(1,10001)}")
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?