title: Count Sum Of Digits In Numbers From 1 To N
signature: class Solution {
  
      public int CountSumOfDigitsInNumbersFrom1ToN(int n) {
test_code: using System;
  using System.Numerics;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.Web.UI;
  using System.Web.Script.Serialization;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  namespace EvaluatePrediction {
      class TestRunner {
          public bool ValidateSolution(int actual, int expected){
              return new JavaScriptSerializer().Serialize(actual) == new JavaScriptSerializer().Serialize(expected);
          }
  
          public string driver(int n, int expected){
              try{
                PLACEHOLDER_CLS_NAME solution_cls = new PLACEHOLDER_CLS_NAME();
                int pred_result = solution_cls.PLACEHOLDER_FN_NAME(n); 
                if (ValidateSolution(
                      pred_result,
                      expected)){
                  return "PASSED";
                }
                return "FAILED";
              } 
              catch (Exception exception_obj)
              {
                return exception_obj.GetType().FullName;
              }
          }
          static void Main(string[] args) {
              TestRunner runner = new TestRunner();
              String result;
              result = runner.driver(
                  29, 
                  165
              );
              Console.WriteLine(string.Format(
                  "TEST-0...{0}",
                  result)
              );result = runner.driver(
                  97, 
                  865
              );
              Console.WriteLine(string.Format(
                  "TEST-1...{0}",
                  result)
              );result = runner.driver(
                  71, 
                  540
              );
              Console.WriteLine(string.Format(
                  "TEST-2...{0}",
                  result)
              );result = runner.driver(
                  82, 
                  667
              );
              Console.WriteLine(string.Format(
                  "TEST-3...{0}",
                  result)
              );result = runner.driver(
                  69, 
                  525
              );
              Console.WriteLine(string.Format(
                  "TEST-4...{0}",
                  result)
              );result = runner.driver(
                  30, 
                  168
              );
              Console.WriteLine(string.Format(
                  "TEST-5...{0}",
                  result)
              );result = runner.driver(
                  82, 
                  667
              );
              Console.WriteLine(string.Format(
                  "TEST-6...{0}",
                  result)
              );result = runner.driver(
                  32, 
                  177
              );
              Console.WriteLine(string.Format(
                  "TEST-7...{0}",
                  result)
              );result = runner.driver(
                  77, 
                  609
              );
              Console.WriteLine(string.Format(
                  "TEST-8...{0}",
                  result)
              );result = runner.driver(
                  39, 
                  240
              );
              Console.WriteLine(string.Format(
                  "TEST-9...{0}",
                  result)
              );
          }
      }
  }
  
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 );
  }