Question: write C program Collatz Conjecture The Collatz Conjecture e was proposed in 1937. It works like this: Take any positive integer n. If n is
write C program

Collatz Conjecture The Collatz Conjecture e was proposed in 1937. It works like this: Take any positive integer n. If n is even, divide it by 2 If n is odd, multiply it by 3 and add 1. Repeat indefinitely. For example, if you start with n = 6, the resulting sequence is 6, 3, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, .... The sequence gets stuck in an endless loop of 4, 2, 1, 4, 2, 1, .... So we say that the sequence ended when it reached 1 and its length is 9. The conjecture is that the sequence will always eventually converge to 1, no matter what value of n is chosen. Interestingly, no one has been able to prove that it is conclusively true or false. However, lots of values of n have been tried and no infinite sequence has yet been found. So we think it is true, but have not been able to prove it. If you find a proof, you'll be famous. They'll even name the proof after you. In the meantime, you'll write a program to generate Collatz sequences. Your program should: Prompt the user to enter a positive integer. If the user enters an invalid integer, re-prompt the user until the number is positive. Print out the Collatz sequence. Print out the length of the sequence. Your program should have three functions: getStart: Prompts the user and returns a valid starting number. It should contain a loop to ensure the number is positive. nextCollatz: Given an integer, generate the next number in the sequence. This function should not generate the whole sequence; rather, just the next number. It also does not print anything out. For example, nextCollatz(12) should return 6 because 12 is even and is divided by 2; nextCollatz(7) should return 22 because 7 is odd. main: Calls getStart to obtain the starting number. Then loops around nextCollatz until the sequence reaches 1, printing each number as it goes. Finally, it prints the length of the sequence. Don't worry about wrapping long lines of output to make it look pretty; just print out the sequence and let the terminal wrap it for you. The program should work like this: Enter the starting number: -5 The number should be a positive integer. Enter the starting number: 11 Collatz sequence: 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 Length: 15 The values in the sequence can get large; you may want to use a long (which is 64 bits on our system) to hold the values
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
