3n+1, C/C++ Programming

Consider the following algorithm to generate a sequence of numbers. Start with an integer n. If n is even, divide by 2. If n is odd, multiply by 3 and add 1. Repeat this process with the new value of n, terminating when n is equal to 1. For example, the following sequence is generated for n = 22:

22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured (but not yet proven) that this algorithm will terminate at n is equal to 1 for every integer n. The conjecture holds for integers up to at least 1,000,000.

For an input n, the cycle-length of n is the number of numbers generated up to and including the 1. In the example above, the cycle length is 16.

Write a C++ program that repeatedly prompts the user for two numbers, Start and End, and determines the maximum cycle length over all numbers between Start and End, including both endpoints. Assume all inputs are integers less than 1,000,000 and greater than 0. If a negative number is entered for the start, then the program should end immediately.

Sample Command Line Interface: (Bold Signifies User Input)
Start: 1
End: 10
Max: 20

Start: 100
End: 200
Max: 125

Start: 201
End: 210
Max: 89

Start: -3
Posted Date: 7/19/2012 8:41:58 PM | Location : United States







Related Discussions:- 3n+1, Assignment Help, Ask Question on 3n+1, Get Answer, Expert's Help, 3n+1 Discussions

Write discussion on 3n+1
Your posts are moderated
Related Questions
Ask question #Minimum 100 words acceptedEducational Objectives: After completing this assignment, the student should be able to accomplish the following: Apply generic algorithms i

When you are designing a generic class to have or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or

Can I drop the [] while deleteing array of some built-in type (char, int, etc)? A: No. you can't Sometimes programmers think that the [] in the delete[] p only present so the

Set of Integers) Create class IntegerSet. Each IntegerSet object can hold integers in the range 0-100. The set is represented by an array of bools. Array element a[i] is true if in

Write a program called plus that will display asterisks in the shape of a plus sign which has the height and width specified by the user. You must only allow entry of an odd numb


Requirements Create a "makefile" that will manage the construction of a program The name of the makefile must be: makefile The make file must produce an executable

Yes. "Memory pools" are useful in many situations. The bad news is that I'll need to drag you through the mire of how it acts before we talk about all the uses. Firstly, recall

Question 1 What are the different steps in executing a C program? Explain Question 2 What are the commonly used input/output functions in C? How are they accessed?

A: A dangling pointer arises while you use the address of an object after its lifetime is end. It may occur in situations such as returning addresses of automatic variables from a