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

「 Pythonで素数定理を可視化する:π(n)/n の収束を確かめよう」様を、 sympyでAI先生に教えてもらいました。

1
Posted at

・ソースコードを丸投げです。私は、わかっていません。申し訳ありません。

オリジナル

@arairuca(Iruca) 様へ。いつもお世話になっております。

(本日の)ChatGPT先生へ

1. 以下を、sympyのソースコードに書き換えて下さい。numpyは使わないで下さい。(ソースコードを省略)
2. ModuleNotFoundError: No module named 'japanize_matplotlib'
3. プログラムが終了しません。
4. findfont: Font family 'IPAexGothic' not found.
5. 上記の私の質問すべてから、Markdownのテキスト形式で、質問リストを出力して。追番を追加して。本質問も含めて。コピーできる形式で。空白行不要です。

いつものsympyで

import sympy as sp
import math
import matplotlib.pyplot as plt

# ----------------------------------------
# 上限
# ----------------------------------------
N = 10**5

# ----------------------------------------
# エラトステネスの篩
# ----------------------------------------
def sieve_of_eratosthenes(limit):

    is_prime = [True] * (limit + 1)

    is_prime[0] = False
    is_prime[1] = False

    for i in range(2, int(math.sqrt(limit)) + 1):

        if is_prime[i]:

            for j in range(i * i, limit + 1, i):
                is_prime[j] = False

    return is_prime


# ----------------------------------------
# 素数判定
# ----------------------------------------
is_prime = sieve_of_eratosthenes(N)

# ----------------------------------------
# データ生成
# ----------------------------------------
x = []
ratio = []

x_theory = []
theoretical = []

count = 0

for n in range(1, N + 1):

    x.append(n)

    if is_prime[n]:
        count += 1

    ratio.append(count / n)

    if n >= 2:

        x_theory.append(n)

        theoretical.append(1 / math.log(n))

# ----------------------------------------
# グラフ
# ----------------------------------------
plt.figure(figsize=(10, 6))

plt.plot(
    x,
    ratio,
    linewidth=0.8,
    label=r'$\pi(n)/n$'
)

plt.plot(
    x_theory,
    theoretical,
    linestyle='--',
    linewidth=1.2,
    label=r'$1/\ln(n)$'
)

plt.xlabel('n')

plt.ylabel(r'$\pi(n)/n$')

plt.title('Prime Number Theorem')

plt.legend()

plt.tight_layout()

plt.savefig(
    f"prime_number_theorem_{N}.png",
    dpi=150
)

plt.show()

実行すると、以下のようなグラフが得られる。

0Figure_1.png

いつもの? sympyの実行環境と 参考のおすすめです。

sympyのweb上での実行方法

SymPy Live Shellで。FreeCADのマクロは、以下で実行できません。

(テンプレート)

いつもと違うおすすめです。

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