A381764 Nearest integer not equal to n with the same Hamming weight (number of 1 bits) as n.
2, 1, 5, 2, 6, 5, 11, 4, 10, 9, 13, 10, 14, 13, 23, 8, 18, 17, 21, 18, 22, 21, 27, 20, 26, 25, 29, 26, 30, 29, 47, 16, 34, 33, 37, 34, 38, 37, 43, 36, 42, 41, 45, 42, 46, 45, 55, 40, 50, 49, 53, 50, 54, 53, 59, 52, 58, 57, 61, 58, 62, 61, 95, 32, 66, 65, 69, 66
Offset: 1
Keywords
Examples
For n = 2, A057168(2) = 4 and A243109(2) = 1 and 1 is closer to 2 than 4, thus a(2) = 1.
Links
- Robert Israel, Table of n, a(n) for n = 1..10000
- Marc B. Reynolds, Popcount walks: next, previous, toward and nearest (2023).
Programs
-
Maple
f:= proc(n) local t,k,d; if n::even then d:=-1 else d:= 1 fi; t:= convert(convert(n,base,2),`+`); for k from n+d by d do if convert(convert(k,base,2),`+`) = t then return k fi od end proc: map(f, [$1..100]); # Robert Israel, Jun 20 2025
-
Python
def A381764(n): return n^((a:=-n&n+1)|(a>>1))
Comments