Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Number of steps to reach 0 or 1, starting with n and applying the map k -> (number of 1's in binary expansion of k) repeatedly.
3

%I #44 Sep 08 2022 08:45:54

%S 0,0,1,2,1,2,2,3,1,2,2,3,2,3,3,2,1,2,2,3,2,3,3,2,2,3,3,2,3,2,2,3,1,2,

%T 2,3,2,3,3,2,2,3,3,2,3,2,2,3,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,1,2,2,3,

%U 2,3,3,2,2,3,3,2,3,2,2,3,2,3,3,2,3,2,2,3,3,2,2,3,2,3,3,3,2,3,3,2,3

%N Number of steps to reach 0 or 1, starting with n and applying the map k -> (number of 1's in binary expansion of k) repeatedly.

%C The number of 1's in binary expansion of n is called the binary weight (or Hamming weight) of n, A000120(n).

%C a(n)=0 for n=0 and n=1; a(n)=1 for powers of 2.

%C Records appear for n = 2, 3, 7, 127=2^7-1, 2^127-1, ... (terms of A007013).

%C It appears that the indices of the even terms for n>0 are sequence A075311.

%H Reinhard Zumkeller, <a href="/A180094/b180094.txt">Table of n, a(n) for n = 0..10000</a>

%p a:= n-> `if`(n<2, 0, 1 + a(add(i, i=convert(n, base, 2)))):

%p seq(a(n), n=0..100); # _Alois P. Heinz_, Jan 15 2011

%t Table[Length[NestWhileList[DigitCount[#,2,1]&,n,#>1&]]-1,{n,0,100}] (* _Harvey P. Dale_, Jul 27 2012 *)

%o (PARI)

%o bitcount(x)=

%o { /* Return Hamming weight of x, i.e. A000120(x) */

%o local(p); p = 0;

%o while ( x, p+=bitand(x, 1); x>>=1; );

%o return( p );

%o }

%o X(n)=

%o { /* Return how many iterations of bitcount() are needed to reach 0 or 1 */

%o if ( n<=1, return(0) );

%o return( 1+X(bitcount(n)) );

%o }

%o { for (n=0, 100, print1(X(n),", ") ); } /* print terms of sequence */

%o (Magma)

%o Countbits:=func< n | &+Intseq(n, 2) >;

%o StepsTo01:=function(n); s:=0; k:=n; while k gt 1 do k:=Countbits(k); s+:=1; end while; return s; end function;

%o [ StepsTo01(n): n in [0..105] ]; // _Klaus Brockhaus_, Jan 15 2011

%o (Haskell)

%o a180094 n = snd $ until ((< 2) . fst) (\(x, c) -> (a000120 x, c+1)) (n,0)

%o -- _Reinhard Zumkeller_, Apr 22 2011

%Y Cf. A000120, A072086.

%Y One less than A078627.

%K nonn

%O 0,4

%A _Joerg Arndt_, Jan 15 2011