PTA: (Basic Level) Practice (中文)

1001 killed attractiveness of the (3n + 1) guess

Kharazi (Callatz) guess :

For any positive integer n, if it is even, then it halved; if it is odd, the (3n + 1) cut by half. This has been repeatedly cut, finally got to get n = 1 in a step. Kharazi at the 1950 World Congress of Mathematicians announced this conjecture, was the legendary Yale University teachers and students Qidong Yuan, desperately want to prove this seemingly silly naive proposition, students inadvertently result so much noise studies, one only card (3n +1), so some people say this is a conspiracy, Kharazi was deliberately delaying the progress of American mathematics teaching and research ......

Our topic today is not evidence Minka Raz conjecture, but on any given positive integer not exceeding 1,000 n, simply count the number, how many steps (cut a few) to get n = 1 need?

Input formats:

每个测试输入包含 1 个测试用例,即给出正整数 n 的值。

Output formats :

输出从 n 计算到 1 需要的步数。

Sample input:

3

Sample output:

5

Code:

def even(num):
    num = num / 2
    return num


def odd(num):
    num = (3 * num + 1) / 2 
    return num


step = 0
n = int(input())
m = n
while n != 1:
    if n % 2 == 0:
        n = even(n)
        step += 1
    else:
        n = odd(n)
        step += 1
print(step)

Guess you like

Origin www.cnblogs.com/CloudGuest/p/11521873.html