title: Count Sum Of Digits In Numbers From 1 To N
signature: function countSumOfDigitsInNumbersFrom1ToN(n) {
test_code: 
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  function convertToString(value) {
      return JSON.stringify(value, function (key, val) {
          if (val instanceof Set) {
              return Array.from(val);
          } return val;});
  }
  
  function validateSolution(actual, expected){
      return convertToString(actual)===convertToString(expected);
  }
  
  function driver(n, expected){
      try{
          if (validateSolution(PLACEHOLDER_FN_NAME(n),expected)){
              return "PASSED";
          }
          return "FAILED";
      } catch (error) {
        return error.name;
      }
      
  }
      
  function main(){
    let result = '';
    result = driver(29, 165);
    console.log(`TEST-0...${result}`);
  
    result = driver(97, 865);
    console.log(`TEST-1...${result}`);
  
    result = driver(71, 540);
    console.log(`TEST-2...${result}`);
  
    result = driver(82, 667);
    console.log(`TEST-3...${result}`);
  
    result = driver(69, 525);
    console.log(`TEST-4...${result}`);
  
    result = driver(30, 168);
    console.log(`TEST-5...${result}`);
  
    result = driver(82, 667);
    console.log(`TEST-6...${result}`);
  
    result = driver(32, 177);
    console.log(`TEST-7...${result}`);
  
    result = driver(77, 609);
    console.log(`TEST-8...${result}`);
  
    result = driver(39, 240);
    console.log(`TEST-9...${result}`);
  
    process.exit(0);
  };
  
  if (require.main === module) {
    main();
  }
source_py: def count_sum_of_digits_in_numbers_from_1_to_n(n):
      if (n < 10):
          return ((n * (n + 1)) / 2)
      d = int(math.log10(n))
      a = ([0] * (d + 1))
      a[0] = 0
      a[1] = 45
      for i in range(2, (d + 1)):
          a[i] = ((a[(i - 1)] * 10) + (45 * int(math.ceil(math.pow(10, (i - 1))))))
      p = int(math.ceil(math.pow(10, d)))
      msd = (n // p)
      return int(((((msd * a[d]) + (((msd * (msd - 1)) // 2) * p)) + (msd * (1 + (n % p)))) + count_sum_of_digits_in_numbers_from_1_to_n((n % p))))
source_cpp: int count_sum_of_digits_in_numbers_from_1_to_n(int n) {
    if ( n < 10 ) return n * ( n + 1 ) / 2;
    int d = log10 ( n );
    int * a = new int [ d + 1 ];
    a [ 0 ] = 0, a [ 1 ] = 45;
    for ( int i = 2;
    i <= d;
    i ++ ) a [ i ] = a [ i - 1 ] * 10 + 45 * ceil ( pow ( 10, i - 1 ) );
    int p = ceil ( pow ( 10, d ) );
    int msd = n / p;
    return msd * a [ d ] + ( msd * ( msd - 1 ) / 2 ) * p + msd * ( 1 + n % p ) + count_sum_of_digits_in_numbers_from_1_to_n ( n % p );
  }