🫠

渦巻きに素数をプロット

に公開

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# 素数判定
def is_prime(n):
    if n < 2: return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0: return False
    return True

N = 10000
numbers = np.arange(1, N + 1)
is_prime_vec = np.array([is_prime(n) for n in numbers])
colors = np.where(is_prime_vec, 'red', 'lightgray')

fig, ax = plt.subplots(figsize=(8, 8), facecolor='white')

def update(frame):
    ax.clear()
    tightness = 0.5 + (frame * 0.01)
    
    theta = tightness * numbers
    r = numbers
    x = r * np.cos(theta)
    y = r * np.sin(theta)
    
    ax.scatter(x, y, c=colors, s=2, alpha=0.8)
    ax.set_title(f"Prime Spiral (tightness = {tightness:.2f})", fontsize=15)
    ax.axis('equal')
    ax.axis('off')

ani = animation.FuncAnimation(fig, update, frames=50, interval=200)

☆テスト前に何やってんの?

Discussion