Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the rule if n is even divide it by 2 otherwise multiply by three and add 1 . For example, if we

Consider the rule "if n is even divide it by 2 otherwise multiply by three and add 1." For example, if we apply the rule to 7 then we would get the new value 22 since 7 is odd and 3*7+1=22. On the other hand, if we apply the rule to 22 we get 11 since 22 is even and 22/2=11.The Collatz conjecture states that any sequence that starts with a positive integer formed by repeatedly applying the above rule will always reach the number 1. For example 7-->22-->11-->34-->17-->52-->13-->40-->20-->10-->5-->16-->8-->4-->2-->1.( Once the number 1 is reached, we'll stop since the following values would be 1-->4-->2-->1 repeating infinitely.) We'll call such a sequence, a Collatz sequence.So far, the Collatz conjecture has not been proved or disproved, so it's useful to investigate the outcome for various starting values.Write a program to generate Collatz sequences with a starting value given by the user, to be stored in a value of type unsigned int. (That's like an int but only stores positive integers.) The program will output the values obtained until 1 is reached (Of course if the Collatz conjecture is false you could get an infinite loop!) The length of the sequence will also be given. To avoid integer overflow, include a defined constant LIMIT in your program with the value one billion. If the next value generate exceeds LIMIT then you should stop the loop and print "Limit exceeded!"The user can continue with more starting values until they decide to quit.Below are some sample runs. Your output should match the sample run output EXACTLY when given the same input. Use a do-while loop for the outer loop (to allow the user to enter more starting values) and a while loop for the inner loop (to generate the sequence). No use of the keyword break is allowed.For your submission, copy paste your code and at the bo

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_2

Step: 3

blur-text-image_3

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

Expert Performance Indexing In SQL Server

Authors: Jason Strate, Grant Fritchey

2nd Edition

1484211189, 9781484211182

More Books

Students also viewed these Databases questions