The Collatz sequence is generated with the following algorithm.

1. Start with an integer n.

2. If n is even, divide by 2.

3. If n is odd, multiply by 3 and add 1.

4. Repeat this process with the new value of n, terminating when n = 1.

For example, starting with n = 11, the following sequence of numbers will be generated: 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1.

Write a MIPS program that prompts the user to enter the starting value n and prints the Collatz sequence.

An example run is shown below:

Enter starting n = 22

Bonus: Implement a recursive version of the Collatz program without using any loops.

Solved By Verified

Study Co-Pilot All Study Co-Pilots are evaluated by Gotit Pro as an expert in their subject area.

Instant

Download

Student review: (3 ratings) 3 out of 3 people found this solution helpful.