Can you code this for me?

Problem 3: The 3n+1 problem (revisited)
We have seen the collatz function in class:
int collatz(int n) {
if (n%2==0)
return n/2;
else
return 3*n+1;}
int main() {
int n;
cin>>n;
while (n!=1)
n=collatz(n);
}
It is conjectured that, if we start with any positive integer, repeatedly appl ying
the collatz function will eventually brings us to 1. So the condition to exit the
while loop will always be met. For instance, if we start with 10, we will go
through the following sequence:
10→5→16→8→4→2→1
(a) Try the above program and input a value for n
equal to 113383. The program seems to enter an infinite loop. Press Ctrl-C to stop it. Is this a counter example that disproves the conjecture? Modify the program to output the intermediate
values in the sequence and try to understand what is happening and how to fix it.
(b) Write a function called collatzCount that accepts an integer n and returns
the number of steps needed to reach 1. For instance, collatzCount(10) should
return 6.
(c) Using part (b), write a program in main to discover which number in[1,1000000] takes the longest to reach 1. How many steps does it require?
Yes we can, but we will not.

Please come back with a question or something you need help with. This is not a site to do homework for people. This forum is to help with problems people may have come across.

*Check out this link please.
http://www.cplusplus.com/forum/beginner/1/
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.
Last edited on
Topic archived. No new replies allowed.