Python可视化 验证角谷定理

Python可视化 验证角谷定理,第1张

前几天在Youtube上 看到了很多数学家都struggle于这个问题的证明上

为此 我写了一个程序来验证考拉兹定理

So 什么是Collatz Conjecture?

代码:

from random import choice
import matplotlib.pyplot as plt

fig  = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_xlabel("number")
ax.set_ylabel("times")

for i in range(1,10):
    times=[]
    numbers=[]
    origin=[]
    time=0
    number = choice(range(50,10000))
    plt.plot(time,number,label=str(number))
    while True:
        plt.plot(times,origin,c="black")
        plt.plot(times,numbers,lw=1.5)
        time+=1
        origin.append(0)
        print(number)
        if number%2==0:
            number=number/2
        else:
            number=3*number+1
        times.append(time)
        numbers.append(number)
        if numbers[len(times)-1] == 1:
            break
plt.legend()
plt.show()

效果图:

 


       

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/570743.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存