以下是验证"鬼谷猜想"的代码:

def count_iterations(num):
    count = 0
    while num != 1:
        if num % 2 == 0:
            num = num // 2
        else:
            num = num * 3 + 1
        count += 1
    return count

测试样例:

print(count_iterations(10))  # 输出结果为 6
print(count_iterations(7))   # 输出结果为 16
print(count_iterations(1))   # 输出结果为 0
鬼谷猜想验证:计算数字循环次数 - Python 代码示例

原文地址: https://www.cveoy.top/t/topic/pHjz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录