3n+1
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.
All i know is that for an iteration, we always have to initalize it. And that we should use a "while do" loop. After that i am stuck
Hmmm it would help if you used \( \LaTeX \)... I'm not sure what is subscript and what is multiplication.
okay, hold okay hold on, what ever is in [] is a subscript: The 3n+1 iteration. For an input integer x0, the “3n+1 iteration” is described as follows: x[k+1] = x[k]/2 , if x[k] is even 3x[k] + 1, if x[k] is odd . The iteration stops at x[k] if it repeats one of the earlier iterates, or x[k] = 1. This is because when an iterate reappears, the iteration will go into a cycle, and if x[k] = 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.
sure, guess you have to make a vector (array) with the past values you've calculated and then compare each new xk with those values... that's the 'while' condition... 'do' is calculating the next iterate... go back and check 'while' : new xk equals any of the older ones (or 1)? no? keep doing.
@Collatzguy what language are we using to program this?
I hadnt thought about using a vector to check the iterates. But i am even still stuck on how to even write the while do loop. Using Maple
hmm, i sort of got an idea brewing in my head
You're going to want a variable that 'counts' each iteration as well as a variable that keeps track of the value of x. You also want a while loop. I've never programmed in maple though.
Or at least it's been a long time.
something like that, probably not the best way to do it, but that's what comes to mind.
do { xk+1 = xk/2 , if xk is even 3xk + 1, if xk is odd } while { xk+1 != [xk, xk-1, xk-2 ...] *this is the vector into which you've* * loaded past xk's; keep count of them* * with and index, use the index to* } * compare xk+1 to each previous result*
why did you put a factorial in that vector
" != " means 'not equal'
dunno what Maple uses for "not equal"
probably something fancier.
okay, you gave me some good ideas, i am gonna try and fly with them.
Join our real-time social learning platform and learn together with your friends!