OFFSET
0,2
COMMENTS
This is the irregular triangle read by rows giving trajectory of n in the Collatz problem, flattened and with all the repeated terms deleted.
This sequence goes to infinity as n gets larger. On the Collatz conjecture this sequence is a permutation of the positive integers. [Corrected by Charles R Greathouse IV, Jul 29 2016]
LINKS
FORMULA
row(n) = {
if seen[n]: stop
else: write(n) and do:
| n is one: stop
| n is odd: n <- 3*n+1
| n is even: n <- n/2
}
EXAMPLE
Triangle begins:
1;
2;
3, 10, 5, 16, 8, 4;
...
The Collatz trajectories for the first five positive integers are {1}, {2, 1}, {3, 10, 5, 16, 8, 4, 2, 1}, {4, 2, 1}, {5, 16, 8, 4, 2, 1}.
From {2, 1} we delete 1 because it has already occurred. From {3, 10, 5, ..., 4, 2, 1} we delete {2, 1} because both numbers have already occurred. We completely get rid of {4, 2, 1} because it has already occurred as the tail end of {3, 10, 5, ...}, and we also completely get rid of {5, 16, 8, ...} for the same reason.
This leaves us with {1}, {2}, {3, 10, 5, 16, 8, 4}, thus accounting for the first eight terms of this sequence.
MATHEMATICA
collatz[n_] := NestWhileList[If[EvenQ[#], #/2, 3 # + 1] &, n, # > 1 &]; DeleteDuplicates[Flatten[Table[collatz[n], {n, 20}]]] (* Alonso del Arte, Oct 24 2015 *)
PROG
(Sidef)
func collatz(n) is cached { # automatically memoized function
say n; # prints the first unseen numbers
n.is_one ? 0
: (n.is_even ? collatz(n/2)
: collatz(3*n + 1));
}
range(1, Math.inf).each { |i| collatz(i) }
CROSSREFS
KEYWORD
nonn,tabf,easy,nice
AUTHOR
Daniel Suteu, Oct 24 2015
STATUS
approved