Skip to content

Instantly share code, notes, and snippets.

@todashuta
Created December 30, 2025 10:18
Show Gist options
  • Select an option

  • Save todashuta/ac452a8944d4a8a2a883ea87485f8f46 to your computer and use it in GitHub Desktop.

Select an option

Save todashuta/ac452a8944d4a8a2a883ea87485f8f46 to your computer and use it in GitHub Desktop.
コラッツの問題の数列ジェネレーター
def collatz_gen(n):
yield n
while n > 1:
if n % 2 == 0:
n = n // 2
else:
n = n * 3 + 1
yield n
#print(list(collatz_gen(6)) == [6, 3, 10, 5, 16, 8, 4, 2, 1])
for i in collatz_gen(6):
print(i)
@todashuta
Copy link
Author

# 1から9999までの数に対して、1にいたるまでのステップ数のグラフ

import matplotlib.pyplot as plt
import numpy as np

a = np.arange(1, 10000, 1)
b = [len(tuple(collatz_gen(i))) for i in a]

plt.scatter(a, b, s=10)
plt.show()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment