Pages

Friday 7 December 2012

UVA - 12517 - Digit Sum (Java Solution)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
   
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        while(true){
            String []str=br.readLine().split(" ");
            int m=Integer.parseInt(str[0]);
            int n=Integer.parseInt(str[1]);
            if(m==0 && n==0){
                break;
            }
            sb.append(sumofDigit(n,1,1)-sumofDigit(m-1,1,1)).append("\n");
        }
        System.out.print(sb);
    }
   
    static long sumofDigit(long n, long ditInd, long poss) {
        if (n == 0) {
            return 0;
        }
        long m = n % 10;
        return n / 10 * 45 * ditInd + m * poss + m * (m - 1) / 2 * ditInd + sumofDigit(n / 10, ditInd * 10, poss + m * ditInd);
    }
}

2 comments:

  1. Hey how did you get to that solution? Any hint would be appreciated :)

    ReplyDelete
  2. There is a formula for such problem think about getting it :) ...

    ReplyDelete