Kakutani Conjecture (Python Implementation)

1. Problem description

Write a function called collatz() that takes an integer parameter num. If the argument is even, collatz() outputs and returns num// 2; if num is odd, collatz() outputs and returns 3 * number + 1. Then write a program to let the user input a positive integer, and call collatz() on this number continuously until the function returns a value of 1

2. Introduction to the Kakutani Conjecture

The Kakutani conjecture was discovered by the Japanese mathematician Shizuo Kakutani. According to research by mathematicians in Japan and the United States, all positive integers less than 7*10^11 conform to this rule. The Kakutani conjecture is also known as the even-even normalization conjecture (English: Collatz conjecture), 3n+1 conjecture, hail conjecture, Kadotani conjecture, Hasse conjecture, Ulam conjecture or Syracuse conjecture, which means that for each positive integer, if it If it is an odd number, multiply it by 3 and add 1, if it is an even number, divide it by 2, and so on, and finally get 1.

3. Python programming implementation

collatz() function:

def collatz(num):
    if num % 2 == 0:
        print(num//2,end='  ')
        return num//2
    else:
        print(3 * num + 1,end='  ')
        return 3*num+1


main() function:

import collatz
def main():
    num=int(input('请输入一个正整数:'))
    n=num
    i=0
    print('角谷猜想的序列是:')
    while n!=1:
        i=i+1
        n=collatz.collatz(n)
        if(i%10==0):
            print()
if __name__=='__main__':
    main()

4. Running results

 

 Well, that's all for sharing!

Guess you like

Origin blog.csdn.net/higerwy/article/details/129400613