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.

ksnctf #20 G00913

Posted at

G00913

πの連続する桁で見つかった最初の10桁の素数を求める問題。

標準のmath.piだと15桁しか表示できない。
mpmathは任意精度演算ライブラリでmpmath.dpsで任意の桁数を指定できる。

素数判定の関数は以下を参考
https://qiita.com/hanaata/items/976ff9aa7bd64203a619

1000桁のπを文字列に変換する。左から10桁の値を素数判定しながら右にずらしていく。
素数が見つかったらbreakでfor文を終了する。

from mpmath import *
mpmath.dps = 100

# 素数判定
def is_prime(n):
    if n == 1: return False

    for k in range(2, int(mpmath.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True


str_pi=str(pi)

for i in range(2,mpmath.dps-10):
    if(is_prime(int(str_pi[i:i+10]))):
        print(str_pi[i:i+10])
        break

まとめ

ほしい関数は調べれば大体出てくる

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?