MATHEMATICS

Sabtu, 16 Juli 2011

Collatz sequence - Revisited

This following Mathematica code prints the Collatz sequence for n to 1.
g[n_]:=NestWhileList[Piecewise[{{3#+1,OddQ[#]},{#/2,EvenQ[#]}}]&,n,#>1&]

For example
g[23]={23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}


Problem 14 of the Euler Project is about the Collatz sequence. Officially it is a conjecture that every sequence starting with n ends in 1.

I spend so much time on 3x+1.... My first blog post in 2005 was on the Collatz sequence. I am able to generate sequences of any length ( thus including very large lengths ) but they end with 1 by definition. It's sort of a paradox.

Tidak ada komentar:

Posting Komentar