title: Count Sum Of Digits In Numbers From 1 To N
signature: class Solution {
  
      public Integer countSumOfDigitsInNumbersFrom1ToN(Integer n) {
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 );
  }
question_info.extension: java
question_info.entry_fn_name: countSumOfDigitsInNumbersFrom1ToN
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 165, "inputs": {"n": 29}}, {"idx": 1, "outputs": 865, "inputs": {"n": 97}}, {"idx": 2, "outputs": 540, "inputs": {"n": 71}}, {"idx": 3, "outputs": 667, "inputs": {"n": 82}}, {"idx": 4, "outputs": 525, "inputs": {"n": 69}}, {"idx": 5, "outputs": 168, "inputs": {"n": 30}}, {"idx": 6, "outputs": 667, "inputs": {"n": 82}}, {"idx": 7, "outputs": 177, "inputs": {"n": 32}}, {"idx": 8, "outputs": 609, "inputs": {"n": 77}}, {"idx": 9, "outputs": 240, "inputs": {"n": 39}}]
question_info.test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(Integer n, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(n,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution(29, 165);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(97, 865);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(71, 540);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(82, 667);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(69, 525);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(30, 168);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(82, 667);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(32, 177);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution(77, 609);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(39, 240);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(Integer n, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(n),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: java __FILENAME__