Question 1-1

Suppose a user executes the following run() method and types the numbers 13.

public static void run() {
    int n = IO.readInt();
    int i = 0;
    while(n != 4) {
        if(n % 2 == 1) n = 3 * n + 1;
        if(n % 2 == 0) n /= 2;
        i++;
    }
    IO.println(i);
}
Show all the values taken on by the variables of the program.
n
i
What does the method print?

Solution

n  13    40 20    10     5    16  8     4
i      0        1     2     3        4     5
It prints ``5.''

Back to 9:40 Quiz 1