3n+1 iteration
The 3n+1 iteration. For an input integer x0, the “3n+1 iteration” is described as follows: xk+1 = xk 2 , if xk is even 3xk + 1, if xk is odd . The iteration stops at xk if it repeats one of the earlier iterates, or xk = 1. This is because when an iterate reappears, the iteration will go into a cycle, and if xk = 1 then the iteration will repeat the pattern 1, 4, 2, 1, 4, 2, 1 after that. When the iteration terminates at x[k], the index k is called the “length” of the iteration while the value of xk is called the terminating value. Write a program that, for input integer x0, outputs a sequence of the 3n + 1 iterates, the length and the terminating value. Use your program to investigate the lengths and terminating values for input x0 from 1 to, say, 100.
Join our real-time social learning platform and learn together with your friends!