Use Python to realize the black hole demonstration of the hail conjecture (3n+1 conjecture, Kakuya conjecture, Sygura conjecture)

Table of contents

1. Origin of hail conjecture

2. Implementation method

1. Define a function that generates new numbers according to the rules

2. Define the function that generates the list

3. Define the main function

3. Complete code

Fourth, part of the code analysis

5. Output result

1. Enter 4

2. Powerful 27

Summarize:


The program demonstration of the two black holes has been completed before, and the demonstration of the hail conjecture is realized with python. If you have any ideas about the program demonstration of the previous two black holes, you can refer to my previous two blog posts.

1. Origin of hail conjecture

One day in 1976, the "Washington Post" reported a piece of mathematics news on the front page. The article narrates such a story:

In the mid-1970s, on the campuses of famous universities in the United States, people were crazy, playing a mathematical game day and night, forgetting to eat and sleep. This game is very simple: write a positive integer N arbitrarily, and transform it according to the following rules:

If it is an odd number, the next step becomes 3N+1.

If it is an even number, the next step becomes N/2.

Not only students, but even teachers, researchers, professors and academics have joined. Why is the charm of this game enduring? Because people found that no matter what number N is, it is impossible to escape back to the bottom 1 in the end. To be precise, it is impossible to escape the 4-2-1 cycle of falling to the bottom, and it will never escape such a fate.

This is the famous "hail conjecture"

The hail conjecture is also known as the Kakutani conjecture, because a Japanese named Kakutani spread it to China.

2. Implementation method

1. Define a function that generates new numbers according to the rules

#生成新数
def generate_new_number(number):
    if number % 2 == 0:
        number = number / 2
    else:
        number = number * 3 + 1
    return int(number) 

Input a positive integer, perform corresponding operations according to parity, and return a new number

2. Define the function that generates the list

#定义计算生成列表的函数
def cal_to_list(input_number):
    number_list =[]     #用于存放过程中的数值
    new_number = generate_new_number(input_number)
    number_list.extend([input_number,new_number])
    #符合条件循环,并将产生的数存入列表,以便查阅统计
    while new_number > 1:
        new_number = generate_new_number(new_number)
        number_list.append(new_number)
    #打印输出
    for item in number_list:
        print(item)
    #打印输出特殊值
    print("进行了{0}次运算".format(len(number_list)-1))
    print("最大的数是{0}".format(max(number_list)))

Call the previous new number generation function to generate a new number, and jump out of the loop according to the situation, and store the result in the list.

3. Define the main function

def main():
    input_number = input("请输入一个正整数!")  # 接收输入的数
    input_int_number = int(input_number)  
    cal_to_list(input_int_number)

    str_begin = input("再来一次,y 或 n ?")
    if str_begin  in ['Y','y','是']:
        main()
    elif str_begin == 'N' or str_begin == 'n':
        print("感谢您的使用!")
    else:
        print("输入错误,会重新开始!")
        main()

Including receiving input data, calling functions, and deciding whether to run the main program again

3. Complete code

"""本程序用来演示冰雹猜想
基本描述:
冰雹猜想 是指:一个正整数x,如果是奇数就乘以3再加1,
如果是偶数就析出偶数因数2ⁿ,这样经过若干个次数,最终回到1。
实现方法:
利用函数判断生成新的数据,调用函数运算不断运算直到符合最小正整数的条件
备注:
以后学习数据分析时,可以生成数据,统计每个正整数的数据情况
"""
#生成新数
def generate_new_number(number):
    if number % 2 == 0:
        number = number / 2
    else:
        number = number * 3 + 1
    return int(number)   #如果此处不加int转化,则会产生的浮点数,没找到为什么.

#定义主函数
def main():
    input_number = input("请输入一个正整数!")  # 接收输入的数
    input_int_number = int(input_number)  # 由于输入的数据直接生成的是字符类的数据,必须先转化为
    cal_to_list(input_int_number)

    str_begin = input("再来一次,y 或 n ?")
    if str_begin  in ['Y','y','是']:
        main()
    elif str_begin == 'N' or str_begin == 'n':
        print("感谢您的使用!")
    else:
        print("输入错误,会重新开始!")
        main()
#定义计算生成列表的函数
def cal_to_list(input_number):
    number_list =[]     #用于存放过程中的数值
    new_number = generate_new_number(input_number)
    number_list.extend([input_number,new_number])
    #符合条件循环,并将产生的数存入列表,以便查阅统计
    while new_number > 1:
        new_number = generate_new_number(new_number)
        number_list.append(new_number)
    #打印输出
    for item in number_list:
        print(item)
    #打印输出特殊值
    print("进行了{0}次运算".format(len(number_list)-1))
    print("最大的数是{0}".format(max(number_list)))

main()

Fourth, part of the code analysis

1. The data string type input by input().

The data input through input is a string type, and it must be converted into an integer type before performing mathematical operations, otherwise an error will occur when the subsequent list comparison calculates the maximum value.

 input_number = input("请输入一个正整数!")  # 接收输入的数
 input_int_number = int(input_number) 

2. Adding multiple elements to the list

When adding multiple list elements to a list,

One method is to use append(), but only one element can be added at a time, and two elements need to be added twice.

The second method is to use extend(), but it needs to combine multiple elements into a list to join

#方法一,利用append()逐个加入
    number_list.append(input_number)
    number_list.append(new_number)
#方法二,利用extend(),组合成列表后加入,内存操作相对较多
    number_list.extend([input_number,new_number])

5. Output result

1. Enter 4

Please enter a positive integer! 4
4
2
1
has been operated twice and
the largest number is 4

2. Powerful 27

Please enter a positive integer! 27
27
82
......
9232
4616
2308
......
2
1
performed 111 operations
and the largest number is 9232

Remarks: The greatest charm of Hail is its unpredictability. John Conway, a professor at the University of Cambridge in the United Kingdom , found a natural number 27. Although 27 is an unattractive natural number, if it is calculated according to the above method, its ups and downs are extremely violent: first, 27 needs to go through 77 steps of transformation to reach the peak value of 9232, and then go through 34 steps to reach the bottom value of 1 . The entire transformation process (called "hail range") requires 111 steps, and its peak value is 9232, which is more than 342 times that of the original number 27. If you compare it with a waterfall-like straight drop (2 to the Nth power), then The number N with the same range reaches 2 to the 111th power. What an astonishing contrast!

Summarize:

Generally speaking, the program is relatively simple to implement, but the analysis of the results is still not in place, it needs to be analyzed, and if possible, mathematical derivation and checking can be carried out. After learning data analysis again, you can analyze the value of each number and summarize the rules.

Guess you like

Origin blog.csdn.net/sygoodman/article/details/124683507