克拉次猜想

滑Google不小心看到的超簡單的爆炸難猜想。
同時也被稱作

3n+1猜想。規則如下:

給定任意正整數

n,對
n
進行以下操作:

如果

n 是偶數(即
n
能被 2 整除),則將
n
除以 2
如果
n
是奇數,則將
n
乘以 3 再加 1
重複這個過程,直到
n
變為 1。
可以用code來模擬這個過程:

import matplotlib.pyplot as plt

def collatz_sequence(n):
    if n <= 0:
        return []  

    sequence = [n]  
    steps = 0 

    while n != 1:
        if n % 2 == 0:
            n = n // 2
        else:
            n = 3 * n + 1
        sequence.append(n)
        steps += 1

    return sequence, steps

def plot_collatz(numbers):
    plt.figure(figsize=(12, 6))
    for n in numbers:
        sequence, steps = collatz_sequence(n)
        plt.plot(sequence, marker='o', label=f'Start: {n} (Steps: {steps})')
    plt.title('Collatz Sequence for Multiple Numbers')
    plt.xlabel('Step')
    plt.ylabel('Value')
    plt.legend()
    plt.grid(True)
    plt.show()


numbers_input = input("請輸入數個正整數,用逗號隔開:")
numbers = [int(num.strip()) for num in numbers_input.split(',')]
plot_collatz(numbers)


Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

可以看到,最後都會收斂在1。嗯,就這樣。

應用?

它主要是數學理論探索的一個範例,用於示範簡單的算術操作如何產生極其複雜的行為。