%I #26 Apr 17 2015 01:32:58
%S 1,1,3,2,2,3,7,3,1,2,4,4,6,7,15,4,4,5,5,1,7,1,8,5,4,5,12,7,14,15,31,7,
%T 6,1,3,1,5,6,9,1,9,10,13,1,15,1,16,6,6,7,6,2,8,9,24,6,12,13,28,15,30,
%U 31,63,11,8,9,3,1,5,6,10,1,9,10,14,1,16,17,17
%N a(n) = smallest k such that a(n-k) and n have the same number of 1's in their binary expansions, or a(n) = n if no such k exists.
%C a(n) = n if and only if n is one less than a power of 2.
%C A257078(n) = smallest number m such that a(m) = n. - _Reinhard Zumkeller_, Apr 16 2015
%H Nathaniel Shar, <a href="/A248756/b248756.txt">Table of n, a(n) for n = 1..8192</a>
%e a(12) = 4. 12 has two 1's in its binary expansion. The previous entry in the sequence that has two 1's in its binary expansion is 3, which is a(8), so a(12) = 12-8 = 4.
%o (PARI) findk(va, n) = {hw = hammingweight(n); for (k=1, n-1, if (hammingweight(va[n-k]) == hw, return (k)););return (0);}
%o lista(nn) = {va = vector(nn); for (n=1, nn, k = findk(va, n); if (k==0, va[n] = n, va[n] = k); print1(va[n], ", "););} \\ _Michel Marcus_, Oct 15 2014
%o (Perl) my (@a, @mem);
%o $a[0] = 0;
%o sub listseq {
%o my $n = shift;
%o for (1..$n) {
%o my $s = digitsum($_);
%o my $last = $mem[$s]||0;
%o $a[$_] = $_-$last;
%o $mem[digitsum($_-$last)] = $_;
%o }
%o print "$_ $a[$_]\n" for 1..$n;
%o }
%o sub digitsum {
%o my $n = shift;
%o my $k = 0;
%o do {$k += ($n&1)} while $n >>= 1;
%o return $k;
%o } # _Nathaniel Shar_, Oct 15 2014
%o (Haskell)
%o a248756 n = a248756_list !! (n-1)
%o a248756_list = f 1 [] where
%o f x yvs = fst yw : f (x + 1) (yw:yvs) where
%o yw = g 1 yvs
%o g _ [] = (x, h)
%o g k ((z,w):zws) = if w == h then (k, a000120 k) else g (k + 1) zws
%o h = a000120 x
%o -- _Reinhard Zumkeller_, Apr 16 2015
%Y Cf. A000120, A007088.
%Y Cf. A257078.
%K nonn,hear,look
%O 1,3
%A _Nathaniel Shar_, Oct 13 2014