OFFSET
1,2
COMMENTS
The Collatz sequence is also called the 3x+1 sequence.
LINKS
Martin Y. Champel, Table of n, a(n) for n = 1..1000
EXAMPLE
Let the C(n) function compute the Collatz sequence starting at n.
For n = 1, C(1) = {1} then term 1 is 1.
For n = 2, C(2) = {1,2} then term 2 is 2.
For n = 3, C(3) = {3,10,5,16,8,4,2,1} = {1,2,3,4,5,8,10,16} then it contains {1,2,3,4,5} but not {1,2,3,4,5,6} then term 3 is 5.
For n = 4, C(4) = C(3) then term 4 is 5.
For n = 5, C(5) = C(4) = C(3) then term 5 is 5.
For n = 6, C(6) = {1,2,3,4,5,6,8,10,16} then term 6 is 6.
MATHEMATICA
Collatz[n_] := NestWhileList[If[EvenQ[#], #/2, 3 # + 1] &, n, # > 1 &]; countConsec[lst_] := Module[{cnt = 0, i = 1}, While[i <= Length[lst] && lst[[i]] == i, cnt++; i++]; cnt]; mx = 0; u = {}; Table[c = Collatz[n]; u = Union[u, c]; mx = Max[mx, countConsec[u]], {n, 65}] (* T. D. Noe, Feb 23 2014 *)
PROG
(Python)
def A235452(n=100):
a = set([])
A235452 = {1: 1}
for i in range(2, n):
c = i
a.add(c)
while c != 1:
if c % 2 == 1:
c = 3 * c + 1
a.add(c)
c = c / 2
a.add(c)
k = 1
while k in a:
k += 1
A235452[i] = k - 1
return A235452
seq_map = A235452()
for n in range(1, len(seq_map) + 1):
print(seq_map[n], end=", ")
CROSSREFS
KEYWORD
nonn
AUTHOR
Martin Y. Champel, Jan 10 2014
STATUS
approved