用户:正在缓冲....查看:1 回复:1 评论:1 创建时间:2021-08-26T15:12:35
冰雹猜想是指:一个正整数,如果是奇数就乘以3再加1,如果是偶数就除以2,在经过若干次的变换之后也必然会到4-2-1的循环。
以下是我在看到 这道看似简单的数学题没有人解得出来【官方双语】【Veritasium真喵素】_哔哩哔哩_bilibili 这个视频后突发奇想写了的代码(第一次发作品帖,见谅)。
import turtle
import math
mypen_1 = turtle.Pen()
mypen_2 = turtle.Pen()
mypen_w = turtle.Pen()
mypen = turtle.Pen()
pen = [mypen_1, mypen_2, mypen, mypen_w]
for i in pen:
i.speed(0)
i.pencolor('#f5f5f7')
myscreen = turtle.Screen()
myscreen.bgcolor('#1d1d1f')
mypen_w.hideturtle()
# 调整窗口大小
height = 800
width = 1200
myscreen.setup(width, height)
# 画坐标系
mypen_w.penup()
mypen_w.goto(width/2-700, height/2-100)
mypen_w.pendown()
mypen_w.write('"3n+1"问题可视化', font=('等线', 20))
mypen_1.penup()
mypen_1.goto(-(width/2-120), height/2-125)
mypen_1.pendown()
mypen_1.write('y', font=('等线', 20))
mypen_1.penup()
mypen_1.goto(-(width/2-50), 0)
mypen_1.pendown()
mypen_1.write('f(n)', font=('等线', 20))
mypen_1.penup()
mypen_1.goto(0, -(height/2-50))
mypen_1.pendown()
mypen_1.write('n', font=('等线', 20))
mypen_1.penup()
mypen_1.goto(-(width/2-150), height/2-125)
mypen_2.left(90)
mypen_2.penup()
mypen_2.goto(-(width/2-150), height/2-125)
mypen_2.pendown()
mypen_1.pendown()
mypen_1.goto(-(width/2-150), -(height/2-100))
mypen_1.goto(width/2-125, -(height/2-100))
mypen_1.write('x', font=('等线', 20))
# 输入
num = int(myscreen.textinput('3n+1', "请输入一个数字"))
num_1 = num
i = 0; x = 0
num_list = []
num_l = []
while num != 1:
if num % 2 == 1:
num = 3*num+1
num_l.append(1)
elif num % 2 != 1:
num = num // 2
num_l.append(0)
i += 1
if x < num:
x = num
num_list.append(num)
print('"3n+1"问题可视化'.format(num_1))
print('\n')
print(num_list)
print('\n')
print('n初始值:{}'.format(num_1))
print('f(n)最大值:{}'.format(x))
print('步骤数:{}'.format(i))
# 文字
mypen_w.penup()
mypen_w.goto(width/2-175, height/2-125)
mypen_w.pendown()
mypen_w.write('n初始值:{}'.format(num_1), font=('等线', 13))
mypen_w.penup()
mypen_w.goto(width/2-175, height/2-150)
mypen_w.pendown()
mypen_w.write('f(n)最大值:{}'.format(x), font=('等线', 13))
mypen_w.penup()
mypen_w.goto(width/2-175, height/2-175)
mypen_w.pendown()
mypen_w.write('步骤数:{}'.format(i), font=('等线', 13))
x_1 = x
i_1 = i
p = 1
p_1 = 1
if x > 15:
l = len(str(x))
p = int(math.pow(10, l-1))
m = x/p
x = math.ceil(m)
if i > 15:
l_1 = len(str(i))
p_1 = int(math.pow(10, l_1-1))
m_1 = i/p_1
i = math.ceil(m_1)
x_list = []
y_list = []
#单位长度
mypen.left(90)
for z in range(i) :
mypen.penup()
n = 855/i*z
mypen.goto(-(width/2-150-855/i-n), -(height/2-100))
x_list.append(-(width/2-150-855/i-n))
mypen.pendown()
mypen.forward(5)
mypen.penup()
mypen.goto(-(width/2-150+3-855/i-n), -(height/2-80))
mypen.pendown()
if (z+1)*p_1 > 99999:
mypen.penup()
mypen.backward(11)
mypen.pendown()
u = (z+1)*p_1
u = len(list(str(u))) -1
mypen.write('{}*10^{}'.format(z+1,u), font=('等线', 12))
else:
mypen.write((z+1)*p_1, font=('等线', 12))
mypen.right(90)
for z in range(x) :
mypen.penup()
n = 580/(x+1)*z
mypen.goto(-(width/2-150), -(height/2-100)+580/(x+1)+n)
y_list.append(-(height/2-100)+580/(x+1)+n)
mypen.pendown()
mypen.forward(5)
mypen.penup()
mypen.goto(-(width/2-110), -(height/2-100)+580/(x+1)+n-7)
mypen.pendown()
if (z+1)*p > 99999:
mypen.penup()
mypen.backward(11)
mypen.pendown()
u = (z+1)*p
u = len(list(str(u))) -1
mypen.write('{}*10^{}'.format(z+1,u), font=('等线', 12))
else:
mypen.write((z+1)*p, font=('等线', 12))
# 数据可视化
mypen.penup()
mypen.goto(-(width/2-150), -(height/2-100)+580/(x+1)*num_1/p)
for n in range (i_1):
mypen.pendown()
mypen.goto(-(width/2-150)+855/i*(n+1)/p_1, -(height/2-100)+580/(x+1)*num_list[n]/p)
turtle.done()