LoginSignup
3
3

コラッツ予想をGoogle Colaboratoryでプロットする

Posted at
import matplotlib.pyplot as plt

def collatz(num, count = 0):
  count += 1

  if num == 1:
    return count

  if num % 2 == 0:
    return collatz(num/2, count)
  else:
    return collatz((num * 3) + 1, count)

# グラフの作成
x = []
y = []

for i in range(1, 30):
  x.append(i)
  y.append(collatz(i))

fig, ax = plt.subplots(dpi=120)
ax.plot(x, y)

plt.show()

コラッツ予想って何?

3
3
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
3
3