Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

a. 22.45% b. 24.40% c. 7.48% d. 10.85% e. 6.98%

Answered: 1 week ago

Question

d. Who are important leaders and heroes of the group?

Answered: 1 week ago