Question 1101136
The number of ones, s,  in the binary representation of  n, <em>when n is {{{ 2^r }}} </em>, is:

    {{{ s = n*(2^n / 2) = n*2^(n-1) }}}

However, 7 digits is reached at 1,000,000 and the closest power of 2 to that is  {{{2^20 = 1048576 }}}
 
So we need to either figure out how to subtract those extra 48577 numbers (48577 because we're including  1,000,000).   

—
 I took a short cut and wrote a C program.   Tested on 2^20 and it gave me 10485760, in agreement with the equation above.   When I feed the program the input  1,000,000 it outputs {{{ highlight(9884992) }}}

—————————————————
C source code below
—————————————————
<pre>
// Took out angle brackets around include files to allow posting, next 3 lines
#include  stdio.h
#include  stdlib.h
#include  strings.h


// Compute how many 1's in binary representation of 0..n

// This array converts index 0..15 to number of 1's in its binary representation
int hdig2ones[16] = { 0, 1, 1, 2, 
                      1, 2, 2, 3,
                      1, 2, 2, 3,
                      2, 3, 3, 4 };

//--------------------------------------
// count ones in hex string
//
int count_ones(char *hs)
{
  int count=0, i, idx;


  for(i=0; i<strlen(hs); i++) {
    if (hs[i] >= 'a') {
      idx = hs[i] - 'a' + 10;
    }
    else {
      idx = hs[i] - '0';
    }
    //printf("input = '%c'\n", hs[i]);
    //printf("n_ones = %d\n", hdig2ones[idx]);
    count += hdig2ones[idx];
  }

  return count;
}

//--------------------------------------
// MAIN
int main(int argc, char **argv) 
{
  int i,j;

  if (argc != 2) {
    printf("ERROR: howmanyones <n>\n");
    exit(1);
  }

  int n = strtol(argv[1],0,0);
  char foo[20];
  int total_ones = 0;

  for(i=0; i<n; i++) {
    // Convert to hex
    sprintf(foo, "%x", i);
    total_ones += count_ones(foo);
  }

  printf("Total 1's written = %d\n", total_ones);

  return 0;    
}