OFFSET
1,1
COMMENTS
Binary weight is given by A000120.
REFERENCES
Donald Knuth, The Art of Computer Programming, Vol. 4A, section 7.1.3, exercises 20-21.
LINKS
Reinhard Zumkeller, Table of n, a(n) for n = 1..10000
M. Beeler, R. W. Gosper, and R. Schroeppel, HAKMEM, MIT Artificial Intelligence Laboratory, Memo AIM-239, February 1972, Item 175 by Gosper, page 81. Also HTML transcription.
Donald E. Knuth, The Art of Computer Programming, Pre-Fascicle 1A, Draft of Section 7.1.3, 2008. Exercises 20 and 21 page 54 and answers pages 75-76.
Marc B. Reynolds, Popcount walks: next, previous, toward and nearest (2023)
FORMULA
EXAMPLE
a(6)=9 since 6 has two one-bits (i.e., 6=2+4) and 9 is the next higher integer of binary weight two (7 is weight three and 8 is weight one).
MATHEMATICA
a[n_] := (bw = DigitCount[n, 2, 1]; k = n+1; While[ DigitCount[k, 2, 1] != bw, k++]; k); Table[a[n], {n, 1, 71}](* Jean-François Alcover, Nov 28 2011 *)
PROG
(PARI) a(n)=my(u=bitand(n, -n), v=u+n); (bitxor(v, n)/u)>>2+v \\ Charles R Greathouse IV, Oct 28 2009
(PARI) A057168(n)=n+bitxor(n, n+n=bitand(n, -n))\n\4+n \\ M. F. Hasler, Aug 27 2014
(Haskell)
a057168 n = a057168_list !! (n-1)
a057168_list = f 2 $ tail a000120_list where
f x (z:zs) = (x + length (takeWhile (/= z) zs)) : f (x + 1) zs
-- Reinhard Zumkeller, Aug 26 2012
(Python)
def a(n): u = n&-n; v = u+n; return (((v^n)//u)>>2)+v
print([a(n) for n in range(1, 72)]) # Michael S. Branicky, Jul 10 2022 after Charles R Greathouse IV
CROSSREFS
KEYWORD
easy,nonn,nice
AUTHOR
Marc LeBrun, Sep 14 2000
STATUS
approved