OFFSET
1,1
COMMENTS
This is the binary analog of A343888.
The required comparisons are (1) that the number of ones in the binary expansion of k must equal the number of ones in the binary expansion of k+n, and (2) that the number of zeroes in the binary expansion of k must equal the number of zeroes in the binary expansion of k+n. See the example below. - Harvey P. Dale, Dec 19 2024
LINKS
Rémy Sigrist, Table of n, a(n) for n = 1..8191
FORMULA
a(n) <= A004757(n).
EXAMPLE
The first terms, alongside the binary expansions of a(n) and of n + a(n), are:
n a(n) bin(a(n)) bin(n+a(n))
-- ---- --------- -----------
1 5 101 110
2 10 1010 1100
3 9 1001 1100
4 20 10100 11000
5 21 10101 11010
6 18 10010 11000
7 17 10001 11000
8 40 101000 110000
9 19 10011 11100
10 42 101010 110100
11 38 100110 110001
12 36 100100 110000
13 37 100101 110010
14 34 100010 110000
15 33 100001 110000
MATHEMATICA
lk[n_]:=Module[{k=1}, While[DigitCount[k, 2, 0]!=DigitCount[n+k, 2, 0]||DigitCount[k, 2, 1]!=DigitCount[n+k, 2, 1], k++]; k]; Array[lk, 70] (* Harvey P. Dale, Dec 19 2024 *)
PROG
(PARI) a(n) = { for (k=1, oo, if (#binary(k)==#binary(n+k) && hammingweight(k)==hammingweight(n+k), return (k))) }
(Python)
def a(n):
k = 1
while k.bit_length() != (n+k).bit_length() or bin(k).count('1') != bin(n+k).count('1'): k += 1
return k
print([a(n) for n in range(1, 62)]) # Michael S. Branicky, May 04 2021
CROSSREFS
KEYWORD
nonn,base
AUTHOR
Rémy Sigrist, May 03 2021
STATUS
approved