部分と全体

知識の巣窟

Python : 素数を計算して表示

素数Pythonに計算させてみた。特に使い道はないけど、グラフ化すると不思議な気分になります。

Prime Numbers

# -*- coding: utf-8 -*-
"""
#素数生成するだけ
Created on Sat Oct  8 15:26:08 2022
@author: kzhat
"""

import math

def is_prime(x):
    if x <= 1:
        return False
    for i in range(2, int(math.sqrt(x)) + 1):
        if x % i == 0:
            return False
    return True

f = int(input('from: '))
t = int(input('to: '))

for i in range(f, t):
    if is_prime(i):
        print(i)

        
#素数のグラフ生成

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(f, t)
y = [is_prime(i) for i in x]
plt.plot(x, y)
plt.show()