title: Count Sum Of Digits In Numbers From 1 To N
signature: int countSumOfDigitsInNumbersFrom1ToN(int n) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(int n, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(n), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver(29, 165); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver(97, 865); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver(71, 540); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver(82, 667); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver(69, 525); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver(30, 168); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver(82, 667); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver(32, 177); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver(77, 609); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver(39, 240); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
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 );
  }