Nios/hailstone

From ASMBits

nios/randomPrevious

A hailstone sequence is a sequence of positive integers generated from a starting number by the following rule:

  • If n is even, n ⇐ n / 2
  • If n is odd, n ⇐ 3n + 1

It has been conjectured that the sequence always reaches 1 for any starting positive integer after some number of steps. Write a function to compute the number of steps required for a given starting number to reach 1. The starting number is a non-zero unsigned integer. You may assume that the sequences tested will not overflow a 32-bit unsigned integer.

unsigned int hailstone (unsigned int n);

Expected solution length: Around 15 lines.

Sample Input

5

Sample Output

5 (The sequence is 5, 16, 8, 4, 2, 1)

Write your solution here

Upload a source file...