Last active
November 9, 2018 07:01
-
-
Save mizunototori/5550e783e2a27ca989a97554b88d839b to your computer and use it in GitHub Desktop.
円周率の中から連続する10桁の最初の素数を見つける ref: https://qiita.com/mizunototori/items/e536f9abbeadd299705a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def is_prime(n): | |
| # nが素数かどうか判定するプログラム | |
| for p in range(2, int(math.sqrt(n)) + 1): | |
| if n % p == 0: | |
| return False # 素数でないならFalseを返す | |
| return True # 素数ならTrueを返す | |
| def get_prime(str_num, N): | |
| # 文字列として受け取ったstr_numからN桁の最初の素数を見つけるプログラム | |
| for i in range(0, len(str_num) - N): | |
| if is_prime(int(str_num[i : i + N])) is True: | |
| print(i,'-th,', str_num[i:i + N] ) | |
| break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >> import math | |
| >> str_pi2 = '3' + str(math.pi)[2:] | |
| >> get_prime(str_pi, 10) | |
| 4 -th, 5926535897 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def is_prime(n): | |
| """nが素数かどうか判定する""" | |
| return all(n % p != 0 for p in range(2, int(n**0.5) + 1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_prime(str_num, N): | |
| """文字列として受け取ったstr_numからN桁の最初の素数を見つける""" | |
| i = next(i for i in range(len(str_num) - N) if is_prime(int(str_num[i:i+N]))) | |
| print(f'{i}-th, {str_num[i:i+N]}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| from decimal import * | |
| def get_pi(N): | |
| # N桁の円周率をガウス=ルジャンドルのアルゴリズムで計算するプログラム | |
| max_iter = 2*math.ceil(math.log2(N)) | |
| getcontext().prec = N | |
| # 初期値の設定 | |
| a_n = 1 | |
| b_n = Decimal(1) / Decimal(2).sqrt() | |
| t_n = Decimal(1) / Decimal(4) | |
| p_n = Decimal(1) | |
| for n in range(0, max_iter): | |
| a_next = Decimal((a_n + b_n) / 2) | |
| b_next = Decimal(a_n * b_n).sqrt() | |
| t_next = Decimal(t_n - p_n * (a_n - a_next) ** 2) | |
| p_next = Decimal(2 * p_n) | |
| a_n = a_next | |
| b_n = b_next | |
| t_n = t_next | |
| p_n = p_next | |
| return Decimal((a_n + b_n) ** 2) / Decimal(4 * t_n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >> pi = get_pi(10000) | |
| >> str_pi = '3' + str(pi)[2:] | |
| >> get_prime(str_pi, 15) | |
| 35 -th, 841971693993751 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment