login
Length of longest contiguous block of 1's in binary expansion of n.
26

%I #47 Jul 05 2022 02:31:23

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

%T 2,1,1,2,3,1,1,1,2,2,2,3,4,2,2,2,2,2,2,2,3,3,3,3,3,4,4,5,6,1,1,1,2,1,

%U 1,2,3,1,1,1,2,2,2,3,4,1,1,1,2,1,1,2,3,2,2,2

%N Length of longest contiguous block of 1's in binary expansion of n.

%H Reinhard Zumkeller, <a href="/A038374/b038374.txt">Table of n, a(n) for n = 1..10000</a>

%H <a href="/index/Bi#binary">Index entries for sequences related to binary expansion of n</a>

%F a(n) >= A089309(n). a(n) >= A089310(n). a(2^i)=1. a(2^i-1)=i. - _R. J. Mathar_, Jun 15 2006

%F May be defined by the recurrence given in A245196, taking G(n)=n+1 (n>=0) and m=1. - _N. J. A. Sloane_, Jul 25 2014

%e a(157) = 3 because 157 in base 2 is 10011101 and longest contiguous block of 1's is of length 3.

%e May be arranged into blocks of lengths 1, 2, 4, 8, 16, ...:

%e 1,

%e 1, 2,

%e 1, 1, 2, 3,

%e 1, 1, 1, 2, 2, 2, 3, 4,

%e 1, 1, 1, 2, 1, 1, 2, 3, 2, 2, 2, 2, 3, 3, 4, 5,

%e 1, 1, 1, 2, 1, 1, 2, 3, 1, 1, 1, 2, 2, 2, 3, 4, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 5, 6,

%e ... - _N. J. A. Sloane_, Jul 25 2014

%p A038374 := proc(n) local nshft,thisr,resul; nshft := n ; resul :=0 ; thisr :=0 ; while nshft > 0 do if nshft mod 2 <> 0 then thisr := thisr+1 ; else resul := max(resul,thisr) ; thisr := 0 ; fi ; nshft := floor(nshft/2) ; od ; resul := max(resul,thisr) ; RETURN(resul) ; end : for n from 1 to 80 do printf("%d,",A038374(n)) ; od : # _R. J. Mathar_, Jun 15 2006

%t Table[Max[Length/@DeleteCases[Split[IntegerDigits[n,2]],_?(MemberQ[ #,0] &)]],{n,120}] (* _Harvey P. Dale_, Jun 10 2013 *)

%o (Haskell)

%o import Data.List (unfoldr, group)

%o a038374 = maximum . map length . filter ((== 1) . head) . group .

%o unfoldr (\x -> if x == 0 then Nothing else Just $ swap $ divMod x 2)

%o -- _Reinhard Zumkeller_, May 01 2012

%o (PARI) a(n)=if (n==0, return (0)); n>>=valuation(n, 2); if(n<2, return(n)); my(e=valuation(n+1, 2)); max(e, a(n>>e)) \\ _Charles R Greathouse IV_, Jan 12 2014; edited by _Michel Marcus_, Apr 14 2019

%o (Python)

%o from itertools import groupby

%o def a(n): return max(len(list(g)) for k, g in groupby(bin(n)[1:]) if k=='1')

%o print([a(n) for n in range(1, 91)]) # _Michael S. Branicky_, Jul 04 2022

%Y Cf. A087117, A090000, A090001, A090002, A090003, A090050, A245196.

%K base,easy,nonn,tabf

%O 1,3

%A _Jeffrey Shallit_