login
Number of squares in {n, f(n), f(f(n)), ...., 1}, where f is the Collatz function.
1

%I #13 Nov 04 2024 15:23:25

%S 1,1,3,2,3,3,3,2,4,3,3,3,3,3,3,3,3,4,3,3,4,3,3,3,4,3,5,3,3,3,5,3,5,3,

%T 3,5,3,3,3,3,5,4,5,3,3,3,5,3,4,4,3,3,3,5,5,3,5,3,3,3,3,5,3,4,5,5,3,3,

%U 3,3,5,5,5,3,5,3,3,3,3,3,4,5,5,4,5,5,3

%N Number of squares in {n, f(n), f(f(n)), ...., 1}, where f is the Collatz function.

%C Or number of squares in the trajectory of n under the 3x+1 map (i.e. the number of squares until the trajectory reaches 1).

%e The finite sequence n, f(n), f(f(n)), ...., 1 for n = 12 is: 12, 6, 3, 10, 5, 16, 8, 4, 2, 1, which has three square terms. Hence a(12) = 3.

%p a:= proc(n) option remember; `if`(n=1, 1,

%p `if`(issqr(n), 1, 0)+a(`if`(n::even, n/2, 3*n+1)))

%p end:

%p seq(a(n), n=1..100); # _Alois P. Heinz_, Nov 04 2024

%t Reap[For[n=1, n <= 100, n++, s=n; t=1; While[s != 1, If[IntegerQ[Sqrt[s]], t++]; If[EvenQ[s], s=s/2, s=3*s+1]]; If[s == 1, Sow[t]]]][[2, 1]] (* _Jean-François Alcover_, Nov 17 2016, adapted from PARI *)

%o (PARI) print1(1, ", ");for(n=2, 100, s=n; t=1; while(s!=1, if(issquare(s), t++, t=t); if(s%2==0, s=s/2, s=(3*s+1)); if(s==1, print1(t, ", "); ); ))

%Y Cf. A006577.

%K nonn

%O 1,3

%A _Michel Lagneau_, Aug 04 2016