编程心得

    技术2022-07-10  95

    未解之谜

    """未解之谜(角谷猜想):一个大于1的整数,如果是偶数,就除以2,如果是奇数,就乘以3再加1, 得到的新数按此方法处理,最终总能得到1。""" # def recursion(z): # if z == 1: # return # next_number = int(z / 2) if z % 2 == 0 else z * 3 + 1 # print(next_number) # recursion(next_number) # # # try: # n = int(input("请输入一个大于1的整数:\n")) # if n <= 1: # print("输入错误!") # exit() # recursion(n) # except ValueError: # print("输入错误!") try: n = int(input("请输入一个大于1的整数:\n")) if n <= 1: print("输入错误!") exit() while n != 1: n = (n % 2 == 0 and int(n / 2) or n * 3 + 1) print(n) except ValueError: print("输入错误!")
    Processed: 0.014, SQL: 9