PAT: 1001 killed attractiveness of the (3n + 1) guess (C language version)

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 format:
Each test comprises a test input, i.e., the value given positive integer n.

Output format:
output calculation from the n-1 to the number of steps required.

Sample input:
3

Sample output:
5

#include <stdio.h>

int isOddnumber(int n);

int main()
{
    int n;
    int count;

    scanf("%d", &n);
    count = 0;
    while(n != 1)
    {
        if (isOddnumber(n))
            n = (3*n + 1) / 2;
        else
            n = n / 2;
        count++;
    }
    printf("%d", count);

    return 0;
}
int isOddnumber(int n)
{
    if (n % 2 != 0)
        return 1;
    
    return 0;
}
Published 24 original articles · won praise 0 · Views 161

Guess you like

Origin blog.csdn.net/qq_45624989/article/details/105041825