Demonstration of Sisyphus black hole (that is, 123 digital black hole) realized by python

Table of contents

1. Description of the 123 digital black hole

2. Problem solving

1. Content description

2. Implementation idea

3. Code function realization

(1) Decompose the input string of numbers to generate a list of numbers

(2) Generate a new string of numbers from a list of numbers

(3) The main program realizes input and output

3. Complete code

Fourth, the result output

1. Verify the previous data

2. Random number string


In the previous analysis and calculation of the Caprecar black hole (rearrangement and difference black hole), if you are interested, you can click the link below to study it!

Using python to realize the calculation of Caprecar black hole (rearrangement and difference black hole)

Next, I will use python to realize the calculation and demonstration of the Sisyphus black hole (that is, the 123 digital black hole).

1. Description of the 123 digital black hole

123 in mathematics is as ordinary and simple as ABC in English. However, the simplest number can be observed with the following sequence of operations .

Values ​​for black holes:

Set an arbitrary number string, count the number of even numbers, odd numbers, and the total number of all digits contained in this number,

For example: 1234567890,

1. Even: Count the even numbers of the number, in this example, 2, 4, 6, 8, 0, there are 5 in total.

2. Odd: Count the odd numbers in the number, in this example, 1, 3, 5, 7, 9, there are 5 in total.

3. Total: count out the total number of digits in the number, 10 in this example.

4. New number: Arrange the answers in the order of "even-odd-total" to get a new number: 5510.

5. Repeat: Repeat the operation of the new number 5510 according to the above algorithm to get a new number: 134.

6. Repeat: Repeat the operation of the new number 134 according to the above algorithm to get a new number: 123.

Conclusion: The logarithm 1234567890, according to the above algorithm, will finally get the result of 123. We can use the computer to write a program to test that any number will be 123 after a limited number of repetitions. In other words, the final result of any number cannot escape from the 123 black hole...

2. Problem solving

1. Content description

(1) Set any number string, count the even number, odd number and all the digits contained in the number, and generate a new number according to the "even-odd-total" bit sequence combination.

(2) Then repeat the above process, and finally get 123.

2. Implementation idea

(1) The program receives and inputs a numeric string

(2) Decompose the input number into a list of numbers

(3) Calculate the even number and odd number, and combine the even number, odd number and the total number into a new number

(4) Repeat the operation on the obtained number until the obtained number no longer changes or the number of digits decreases

(5) Output the data each time in turn for easy checking

3. Code function realization

(1) Decompose the input string of numbers to generate a list of numbers

#分解输入的数字字符串
def split_number(number):
    list_number = []
    str_number = str(number)
    for item in str_number:
        list_number.append(int(item))
    return list_number

The realization of the function is basically the same as that of the previous black hole, so it will not be described in detail here.

(2) Generate a new string of numbers from a list of numbers

The input is the list of numbers generated by the above program, and the output is a new number character

#由数字列表生成新的数字字符串
def list_to_newstring(list_number):
    #依次取出数据进行偶数、奇数的计数
    count_even = 0   #存放偶数个数数值
    count_odd = 0    #存放奇数个数数值
    for item in list_number:
        if item % 2 == 0:
            count_even += 1
        else:
            count_odd += 1
    newstring =str(count_even) +str(count_odd) +str(count_even+count_odd)
    return newstring

Take out the numbers in the number list in turn, judge the parity, accumulate the parity number, and generate a new string

(3) The main program realizes input and output

print("请输入由数字组成的字符串,位数及重复均无要求")
    input_string = input()
    # 分解数据,生成列表
    list_number01 = split_number(input_string)
    result_list = []  # 写入结果值
    hole_list = []  # 写入循环的结果,即为黑洞值
    result_list.append(input_string)
    while True:
        #根据列表生成新数
        new_string = list_to_newstring(list_number01)
        if new_string not in result_list:
            result_list.append(new_string)
            #新数产生新列表
            list_number01 = split_number(new_string)
        else:
            break
    #输出结果列表
    print("输出结果为:")
    for item in result_list:
        print(item)

When duplicate data is found, the loop is stopped immediately, and the result is saved in the result_list list, which is finally used for output.

3. Complete code

"""程序用于演示西绪福斯黑洞(即123数字黑洞)
内容描述:
设定任意数字字符串,数出这个数的偶数个数,奇数个数和数中包含的所有位数,并按”偶-奇-总“位序组合生成新数,
然后重复上述过程,最后得到123。
方法:
程序输入一个数字字符串,程序会按照下面进行演算:
将输入的数字分解为数字列表,计算出偶数、奇数,将偶数、奇数和总数组合成一个新数,
将得出的数进行重复操作,直到得到的数不再变化或位数变少为止
依次输出每次的数据方便验算
"""
#分解输入的数字字符串
def split_number(number):
    list_number = []
    str_number = str(number)
    for item in str_number:
        list_number.append(int(item))
    return list_number

#由数字列表生成新的数字字符串
def list_to_newstring(list_number):
    #依次取出数据进行偶数、奇数的计数
    count_even = 0   #存放偶数个数数值
    count_odd = 0    #存放奇数个数数值
    for item in list_number:
        if item % 2 == 0:
            count_even += 1
        else:
            count_odd += 1
    newstring =str(count_even) +str(count_odd) +str(count_even+count_odd)
    return newstring


def main():    # 提示按要求输入整数
    print("请输入由数字组成的字符串,位数及重复均无要求")
    input_string = input()
    # 分解数据,生成列表
    list_number01 = split_number(input_string)
    result_list = []  # 写入结果值
    hole_list = []  # 写入循环的结果,即为黑洞值
    result_list.append(input_string)
    while True:
        #根据列表生成新数
        new_string = list_to_newstring(list_number01)
        if new_string not in result_list:
            result_list.append(new_string)
            #新数产生新列表
            list_number01 = split_number(new_string)
        else:
            break
    #输出结果列表
    print("输出结果为:")
    for item in result_list:
        print(item)

main()


Fourth, the result output

1. Verify the previous data

Verify the data mentioned above, and the results of the operation are as follows:

Please enter a string composed of numbers, with no digits and repetitions required.
1234567890
The output result is:
1234567890
5510
134
123

2. Random number string

Please enter a character string consisting of numbers, with no digits and repetitions required
.




data analysis:

First time: 43 1s, 0 for even, 43 for odd, total 43, so the new number is 04343

Second time: 3 even numbers, 2 odd numbers, the total is 5, so the new number is 325

Third time: 1 even number, 2 odd numbers, the total is 3, so the new number is 123

The fourth time: 1 even number, 2 odd numbers, the total is 3, so the new number is 123, repeat exit, get the result

The black hole number is 123, the result is correct

Guess you like

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