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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started