Function[n, If[EvenQ[n], n/2, 3 n + 1]]
How can I plot this?
Rohit,
Yep, popped right up.
Eric,
ListPlot[{Log[NestList[Function[n, If[EvenQ[n], n/2, 3 n + 1]], 27, 111]]}, Joined -> True, PlotRange -> Automatic, ImageSize -> Large]
The plot created by this is what I was looking for. I don't know what to do with DiscretePlot to make it look like the plot from above.
Nelson
You probably wouldn't bother. You asked how to plot a function. It's a function defined for integers. DiscretePlot is a nice way to plot such functions. Nowhere in your question did you indicate that you wanted to plot the nested application of your function, that the plot should be a connected line, or that you wanted to apply Log to it. I tried to answer the question as it was asked.
Another way
ResourceFunction["Collatz"][27] // Log // ListLinePlot
How can I get a nested list of, say, the first 20 sequences? Or for that matter, the second 20 sequences.
Are these what you are looking for?
Table[ResourceFunction["Collatz"][i],{i,1,20}]
and
Table[ResourceFunction["Collatz"][i],{i,21,40}]
Pretty much. I was hoping to end up with something shorter than this:
Sort[DeleteDuplicates[Flatten[Table[ResourceFunction["Collatz"][n], {n, 1, 20}]]]]
Union[Flatten[ResourceFunction["Collatz"]/@Range[20]]]
That'll do it! Thanks.
Couldn't get this to work, but with my level of expertise that doesn't mean much. Thanks.
DiscretePlot[Function[n, If[EvenQ[n], n/2, 3 n + 1]][x], {x, -10, 10}]
Maybe try DiscretePlot.
Perhaps this
m=6721; list=Reap[ Sow[m]; Do[ m=Function[n, If[EvenQ[n], n/2, 3 n + 1]][m]; Sow[m], {25}] ][[2,1]]; ListPlot[list,Joined->True]
Or simpler
ListPlot[NestList[Function[n, If[EvenQ[n], n/2, 3 n + 1]],6721,25],Joined->True]
Bill,
Perfect, just what I wanted. Thanks.