Question
Make a program in C ++, using a while loop as an iterative statement. Given a positive integer N, the problem 3n + 1 consists
Make a program in C ++, using a while loop as an iterative statement.
Given a positive integer N, the problem 3n + 1 consists of determining whether it is possible to arrive at N = 1 by repeatedly applying the following operations:
If N is even, divide it by 2. If N is odd, multiply it by 3 and add 1 to it.
For example, if N starts at 26 then N takes the values 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1. It is believed that it is always possible to get to 1 no matter what positive integer you start at (Collatz's Conjecture).
Write a program that calculates the number of steps it takes for N to become 1 and the largest value that N takes during the calculation.
Entry An integer N. You can assume that 1 N 1000.
Exit Two integers denoting the number of steps it takes for N to become 1 and the largest value that N takes during computation.
Example:
Entry -one -26 -27
Exit -0 1 -10 40 -111 9232
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