5.13 · exercise

Collatz countdown

Start from a whole number n. If n is even, halve it; if it is odd, make it 3n + 1. Repeat, and n always seems to reach 1 in the end (nobody has proved it, and nobody has found a number that does not). Count the steps it takes: for 27 the program prints steps = 111.

The starter reads n into n_r and prints steps_r at the end. Write the loop between them, and test before each step, the way a pre-test loop does: when n starts at 1 it is already there, so the answer is 0 steps.

A number below 1 never gets there (0 halves to 0 forever), so for those print n must be at least 1 and return 1 from main.

Hint: udiv then msub gives the remainder of n divided by 2, which tells you whether n is even.

What is checked

  • with the input 27 the program prints steps = 111 and returns 0
  • the checker also tries values you do not see, including 1, numbers below 1, and one whose path climbs past what 32 bits can hold

specification

stdin27
stdoutprints the right output
exitexits with the right code
hiddenright output and exit code on 6 more inputs you do not see

We run your program on the input above and on the hidden ones, and compare what it does. Nothing is matched against a stored solution.

loading editor...

regfile

N clearZ clearC clearV clear

x0–x30 are the integer registers.

X0arg00x0000000000000000
X1arg10x0000000000000000
X2arg20x0000000000000000
X3arg30x0000000000000000
X4arg40x0000000000000000
X5arg50x0000000000000000
X6arg60x0000000000000000
X7arg70x0000000000000000
X8ind0x0000000000000000
X90x0000000000000000
X100x0000000000000000
X110x0000000000000000
X120x0000000000000000
X130x0000000000000000
X140x0000000000000000
X150x0000000000000000
X16ip00x0000000000000000
X17ip10x0000000000000000
X18pr0x0000000000000000
X190x0000000000000000
X200x0000000000000000
X210x0000000000000000
X220x0000000000000000
X230x0000000000000000
X240x0000000000000000
X250x0000000000000000
X260x0000000000000000
X270x0000000000000000
X280x0000000000000000
X29fp0x0000000000000000
X30lr0x0000000000000000
SP0x0000000080000000
PC0x0000000000400000
console

Output prints here as your program runs.

Press step or run under the editor, or feed stdin from the box below.

not assembled

Open in playground