Taking advantage of the 4, 2, 1
cycle. (Assuming the conjecture is correct.)
step = If[EvenQ@#, #/2, 3 # + 1] &;
step3 = Rest@NestList[step, Last@#, 3] &;
Flatten@FixedPointList[step3, {5}] ~Drop~ -3
(* {5, 16, 8, 4, 2, 1} *)
This is a needlessly complicated solution intended to highlight some of Mathematica's functionality and syntax.
For
Reals
: some notes about using a loop (which you shouldn't use in Mathematica: see belisarius
's correct solution) to solve this problem.
First: n
is the index of your sequence of numbers. They just tell you where you are in the sequence. You need to have a variable that represents the value, and you update that variable at every step. So:
val = 5;
For[n = 1, n <= 20, n++,
If[EvenQ[val], val = val/2, val = 3*val + 1]
]
will construct the sequence. Unfortunately, it doesn't Print
out the values, and it doesn't keep the values either. For this, you can do a number of things. For instance,
val = 5;
For[n = 1, n <= 8, n++
, If[EvenQ[val], val = val/2, val = 3*val + 1];
Print[val]
]
This will print the numbers to the screen. Now, we can do better. Perhaps we want to write the numbers to a list?
vals = {5};
For[n = 1, n <= 8, n++,
If[EvenQ[Last@vals], AppendTo[vals, Last@vals/2], AppendTo[vals, 3*Last@vals + 1]]]
vals
(* {5, 16, 8, 4, 2, 1, 4, 2, 1} *)
This is standard sort of programming stuff: building a list by appending values to the list (albeit dynamically, which is not usually where people start when they first start learning programming). However, once again, this is decidedly not the best way to do things in Mathematica.
We can use recursion and memoization:
step[n_] := step[n] = If[EvenQ@step[n - 1], step[n - 1]/2, 3 step[n - 1] + 1]
step[1] = 5;
Array[step, 9]
(* {5, 16, 8, 4, 2, 1, 4, 2, 1} *)
Look up the function Array
in the documentation.
Alternatively, we can define a function which takes an element of the sequence and spits out the next one:
f[x_Integer] := If[EvenQ[x], x/2, 3x + 1]
Then, we can Nest
this function (Nest
essentially performs function composition f[f[f[5]]]
):
NestList[f, 5, 8]
(* {5, 16, 8, 4, 2, 1, 4, 2, 1} *)
Alternatively, we can define this as a pure function, which is how belisarius
did it, who is also taking advantage of the fact that we "know" (read: suspect) that this sequence always eventually reaches 1 for any initial input. In that case, he used NestWhileList
which will go until some condition is met. (FixedPointList
is a variant of this that will keep calculating until the result doesn't change anymore.)