OFFSET
1,3
COMMENTS
The function a(n) is a measure of how many times n rounds up (assigned value +1), down (assigned value -1), or not at all (assigned value 0) when divided by incremental powers of two (see example below).
For n = 2^k, all divisions give integers (not rounded at all) until n/2^(k+1), which rounds up to 1, and so a(2^k) = 1.
n/2^i shifts the bits of n down so the bit of n at position i-1 (least significant bit as position 0) is immediately below the radix point and so determines whether frac(n/2^i) >= 1/2 or < 1/2. frac(n/2^i) = 0 is when the bits of n at i-1 and below are all 0's. So a(n) is sum +1 for each 1-bit of n and -1 for each 0-bit of n but excluding any low 0's. - Kevin Ryde, Aug 31 2020
FORMULA
EXAMPLE
For n = 10, a(10) = 0 + 1 + (-1) + 1 = 1.
MATHEMATICA
Array[2 DigitCount[#, 2, 1] + IntegerExponent[#, 2] - Floor[Log2[#]] - 1 &, 80] (* Michael De Vlieger, Sep 01 2020 *)
PROG
(JavaScript)
var k = 1;
var r = 0;
for (var i = 0; i < 100; i += 1) {
while ((i+1) >= Math.pow(2, k - 1)) {
if (Math.round((i+1) / Math.pow(2, k)) < ((i+1) / Math.pow(2, k))) {
r -= 1;
} else if (Math.round((i+1) / Math.pow(2, k)) > ((i+1) / Math.pow(2, k))) {
r += 1;
} else {
r += 0;
}
k += 1;
}
document.write(r, ", ");
k = 1;
r = 0;
}
(PARI) a(n) = sum(k = 1, 1+logint(n, 2), my(x=(n % 2^k)/2^k); sign(round(x) - x)); \\ Michel Marcus, Aug 23 2020
(PARI) a(n) = 2*hammingweight(n) + valuation(n, 2) - logint(n, 2) - 1; \\ Kevin Ryde, Aug 29 2020
CROSSREFS
KEYWORD
sign
AUTHOR
Christoph B. Kassir, Aug 23 2020
STATUS
approved