I am new to Mathematica and was trying to make a function that lists the Collatz sequence stating with a given number, but I can´t get it to work. Here is my function:
collatz[x_] := NestWhileList[If[Mod[x, 2] == 0, x/2, x*3 + 1], x, x != 1]
I am new to Mathematica and was trying to make a function that lists the Collatz sequence stating with a given number, but I can´t get it to work. Here is my function:
collatz[x_] := NestWhileList[If[Mod[x, 2] == 0, x/2, x*3 + 1], x, x != 1]
x is just your starting value for the iteration. The function to apply, and the stopping condition, should both be applied to the current value obtained by the iteration, and are probably best expressed using pure functions:
collatz[x_] := NestWhileList[If[Mod[#, 2] == 0, #/2, #*3 + 1] &, x, # != 1 &]
collatz[5]
(* Out: {5, 16, 8, 4, 2, 1} *)
This topic has been discussed in some detail on MMA.SE. You could start here: Collatz optimization
You need anonymous functions. The way it's written currently will replace every instance of x on the right hand side with the number you provide as the argument for collatz. What you want is something which can act on the value produced by NestWhileList on each iteration. So use
Clear[collatz]
collatz[x_] :=NestWhileList[If[Mod[#, 2] == 0, #/2, #*3 + 1]&, x, # != 1&]
What's happening here is that the & tells the MMA interpreter that everything preceeding it is an anonymous (or inline) function and the # will all be replaced by the first argument of that anonymous function. To make it more general,
NestWhileList[f[#]&,x,g[#]&]
NestWhileList applies f to x on the first iteration and evaluates f[x]. Next it applies g and expects a Boolean results so if g[f[x]] is False it stops. If True it goes on to the next iteration, now applying f again to the previous result, evaluating now f[f[x]] and continues until g[f[...f[x]...] evaluates false.