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
Dynamic Initialization of objects: It is initializing the objects by passing the valued to the constructor from the user input or other means.   Through cin operator a value


Car Rental System This system tracks cars in a rental company. Each car has a number (assume its plate number), type (small car, four wheel car), and status (rented, available). T

Use the program called harris-shell.c and add some code to find the corners in the image checkers.jpg. You should say that a pixel in the image is a corner if it passes the given t

#question.gsdjfhjhjfkfk.

Aim: To implement a program to maintain information of employees in an educational institute using inheritance. Code:                       class staff {

.Develop a two dimensional interactive game with the following features: 1. Use OpenGL (any version you find convenient) 2. The game can either be two player or one player with the

THIS PROGRAM IS TO MULTIPLY THE TWO COMPLEX NO.S GIVEN BY THE USER #include #include #include struct complex     {     int real;     int imag;     }; void main()  {  clrs

answer for tcs is working question

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec