Question: Given a positive integer n, the following rules will always create a sequence that ends with 1, called Collatz Conjecture (also known as the hailstone

Given a positive integer n, the following rules will always create a sequence that ends with 1, called Collatz Conjecture (also known as the hailstone sequence):

- Continue until n is 1:

- If n is even, divide it by 2

- If n is odd, multiply it by 3 and add 1 (i.e. 3n +1)

Write a program to find the maximum number of steps it takes to transform a given integer to 1 using Collatz Conjecture.

Example: If the input is: 3 The hailstone sequence is 3, 10, 5, 16, 8, 4, 2, 1. Which means it takes 7 steps to reach 1 from 3.

Note: Your program will NOT output the actual sequence, only the step count. You may assume the user gives valid input.

-- Please use C++ --

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!