title: Count Total Set Bits In All Numbers From 1 To N
signature: function count_total_set_bits_in_all_numbers_from_1_to_n(n)
source_py: def count_total_set_bits_in_all_numbers_from_1_to_n(n):
      i = 0
      ans = 0
      while ((1 << i) <= n):
          k = 0
          change = (1 << i)
          for j in range(0, (n + 1)):
              ans += k
              if (change == 1):
                  k = (not k)
                  change = (1 << i)
              else:
                  change -= 1
          i += 1
      return ans
source_cpp: int count_total_set_bits_in_all_numbers_from_1_to_n(int n) {
    int i = 0;
    int ans = 0;
    while ( ( 1 << i ) <= n ) {
      bool k = 0;
      int change = 1 << i;
      for ( int j = 0;
      j <= n;
      j ++ ) {
        ans += k;
        if ( change == 1 ) {
          k = ! k;
          change = 1 << i;
        }
        else {
          change --;
        }
      }
      i ++;
    }
    return ans;
  }
question_info.test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(n, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(n) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver(90, 279)
      print(string.format("TEST-0...%s",result))
  
      result = driver(56, 159)
      print(string.format("TEST-1...%s",result))
  
      result = driver(43, 112)
      print(string.format("TEST-2...%s",result))
  
      result = driver(31, 80)
      print(string.format("TEST-3...%s",result))
  
      result = driver(77, 231)
      print(string.format("TEST-4...%s",result))
  
      result = driver(35, 88)
      print(string.format("TEST-5...%s",result))
  
      result = driver(43, 112)
      print(string.format("TEST-6...%s",result))
  
      result = driver(66, 197)
      print(string.format("TEST-7...%s",result))
  
      result = driver(15, 32)
      print(string.format("TEST-8...%s",result))
  
      result = driver(95, 304)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 279, "inputs": {"n": 90}}, {"idx": 1, "outputs": 159, "inputs": {"n": 56}}, {"idx": 2, "outputs": 112, "inputs": {"n": 43}}, {"idx": 3, "outputs": 80, "inputs": {"n": 31}}, {"idx": 4, "outputs": 231, "inputs": {"n": 77}}, {"idx": 5, "outputs": 88, "inputs": {"n": 35}}, {"idx": 6, "outputs": 112, "inputs": {"n": 43}}, {"idx": 7, "outputs": 197, "inputs": {"n": 66}}, {"idx": 8, "outputs": 32, "inputs": {"n": 15}}, {"idx": 9, "outputs": 304, "inputs": {"n": 95}}]
question_info.extension: lua
question_info.entry_fn_name: count_total_set_bits_in_all_numbers_from_1_to_n
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__