%I #28 Aug 06 2023 11:15:07
%S 4,4,16,4,16,16,52,8,52,16,52,16,40,52,160,16,52,52,88,20,64,52,160,
%T 24,88,40,9232,52,88,160,9232,32,100,52,160,52,112,88,304,40,9232,64,
%U 196,52,136,160,9232,48,148,88,232,52,160,9232,9232,56,196,88,304,160,184
%N In repeated iterations of function m -> m/2 if m even, m -> 3m+1 if m odd, a(n) is maximum value achieved if starting from n.
%C If a(n) exists (which is the essence of the "3x+1" problem) then a(n) must be a multiple of 4, since if a(n) was odd then the next iteration 3*a(n)+1 would be greater than a(n), while if a(n) was twice an odd number then the next-but-one iteration (3/2)*a(n)+1 would be greater.
%C The variant A025586 considers the trajectory ending in 1, by definition. Therefore the two sequences differ just at a(1) and a(2). - _M. F. Hasler_, Oct 20 2019
%H <a href="/index/3#3x1">Index entries for sequences related to 3x+1 (or Collatz) problem</a>
%e a(6) = 16 since iteration starts: 6, 3, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, ... and 16 is highest value.
%p a:= proc(n) option remember; `if`(n=1, 4,
%p max(n, a(`if`(n::even, n/2, 3*n+1))))
%p end:
%p seq(a(n), n=1..88); # _Alois P. Heinz_, Oct 16 2021
%t a[n_] := Module[{r = n, m = n}, If[n <= 2, 4, While[m > 2, If[OddQ[m], m = 3*m + 1; If[m > r, r = m], m = m/2]]; r]];
%t Table[a[n], {n, 1, 100}] (* _Jean-François Alcover_, May 20 2022 *)
%o (PARI) a(n)=my(r=max(4,n));while(n>2,if(n%2,n=3*n+1;if(n>r,r=n),n/=2));r \\ _Charles R Greathouse IV_, Jul 19 2011
%Y Cf. A006370, A056957, A056958.
%Y Essentially the same as A025586.
%K nonn
%O 1,1
%A _Henry Bottomley_, Jul 18 2000