# Computing Digit Sum of All Numbers From 1 to n in Java

In this section, we will discuss the methods to compute digit sum of all numbers from 1 to n in through Java program.

Example:

Input: num = 7

Output:

Sum of all the digits occurring in the numbers from 1 to 7 is:

1 + 2 + 3 + 4 + 5 + 6 + 7 = 28

Input: num = 17

Output:

Sum of all the digits occurring in the numbers from 1 to 17 is:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 1 + 0 + 1 + 1 + 1 + 2 + 1 + 3 + 1 + 4 + 1 + 5 + 1 + 6 + 1 + 7 = 81

Explanation:

In the above example, 10 got split into 1 + 0, 11 got split into 1 + 1, 12 got split into 1 + 2, 13 got split into 1 + 3, and so on.

## Naive Approach

In this approach, we will split every number into its digits, and then we will compute the sum of its digits. Observe the following program.

FileName: ComputeSum.java

Output:

```The sum of digits of numbers from 1 to 17 is: 81
The sum of digits of numbers from 1 to 340 is: 3367
The sum of digits of numbers from 1 to 5 is: 15
```

## Efficient Approach

The efficient approach is a bit tricky. It requires some observation to find the trick. Let's observe some patterns that finds the sum of digits.

From 1 to 9, it is easy to compute the sum, as we can use the mathematical formula of the sum of the first n natural number, which is (n x (n + 1) / 2). Note that split of the digit cannot occur in a single-digit number.

Therefore,

1 + 2 + 3 + … + 9 = (9 x (9 + 1)) / 2 = (9 x 10) / 2 = 90 / 2 = 45.

Implementation of this mathematical formula is easy, and its time complexity is O(1).

Now, let's try to find the sum of digits up to 99. In order to compute till the number 99, one can split this task into the smaller tasks as mentioned below.

Sum of digits of numbers from 1 to 9 = 45

+

Sum of digits of numbers from 10 to 19

+

Sum of digits of numbers from 20 to 29

+

Sum of digits of numbers from 30 to 39

+

Sum of digits of numbers from 40 to 49

+

Sum of digits of numbers from 50 to 59

+

Sum of digits of numbers from 60 to 69

+

Sum of digits of numbers from 70 to 79

+

Sum of digits of numbers from 80 to 89

+

Sum of digits of numbers from 90 to 99

Now, to compute the sum of digits of numbers from 10 to 19, we have to split the numbers as mentioned in the following table.

Table 1:

Number First Digit Second Digit
10 1 0
11 1 1
12 1 2
13 1 3
14 1 4
15 1 5
16 1 6
17 1 7
18 1 8
19 1 9

The sum of the numbers present in the column first digit is 10, and the sum of the numbers present in the column second digit is (9 x (9 + 1)) / 2 = (9 x 10) / 2 = 90 / 2 = 45

Thus, the sum of digits of the numbers from 10 to 19 is 10 + 45.

Now, we will compute the sum of digits of numbers from 20 to 29. Similar to the above table, we have created the following one.

Table 2:

Number First Digit Second Digit
20 2 0
21 2 1
22 2 2
23 2 3
24 2 4
25 2 5
26 2 6
27 2 7
28 2 8
29 2 9

The sum of the numbers present in the column first digit is 20, and the sum of the numbers present in the column second digit is (9 x (9 + 1)) / 2 = (9 x 10) / 2 = 90 / 2 = 45

Thus, sum of digits of the numbers from 20 to 29 is 20 + 45.

Note that the column containing the second digit is the same in both the tables. Also, we see a pattern.

For 10 to 19, we have 10 + 45

For 20 to 29, we have 20 + 45

Similarly, for 30 to 39, we will get 30 + 45. For 40 to 49, we get 40 + 45.

Now sum of digits of numbers from 1 to 99 will be

45 + 10 + 45 + 20 + 45 + 30 + 45 + 40 + 45 + … + 90 + 45

= 45 x 10 + 10 + 20 + 30 + 40 + … + 90

= 45 x 10 + 10 x (1 + 2 + 3 + 4 + … + 9) = 45 x 10 + 10 x ((9 x (9 + 1)) / 2)

= 45 x 10 + 10 x (45)

= (sum of the first 9 natural numbers) x 10 + 45 x 10

Thus, sumDig(99) = sumDig(9) x 10 + 45 x 10

Where sumDig(n) is a method that computes the sum of digits of numbers from 1 to n. Similarly,

sumDig(999) = sumDig(99) x 10 + 45 x 100

In general, sumDig(10n - 1) = sumDig(10n - 1 - 1) x 10 + 45 x (10n - 1)

## Algorithm

On the basis of the formula derived above, we will write an algorithm to compute the sum of digits of numbers from 1 to 328.

Step 1: Split the number 328 to 1 to 299 and 300 to 328.

Step 2: Compute the sum of digits of numbers from 1 to 299. From 1 to 299, we see 99 three times: 99, 199, and 299. Therefore, the sum of digits of numbers from 1 to 99 will also repeat three times. Thus, the sum of digits of numbers from 1 to 299 is = 3 x sum(99) + (1 + 2) x 100.

Step 3: Compute the sum of digits of numbers from 300 to 328. The sum is equal to 3 x 29 + sumDig(28).

Similarly, we can approach other numbers too.

## Algorithm

Based on the above algorithm, the following code is written.

FileName: ComputeSum1.java

Output:

```The sum of digits of numbers from 1 to 17 is: 81
The sum of digits of numbers from 1 to 328 is: 3241
The sum of digits of numbers from 1 to 5 is: 15
```

Time-complexity: The time-complexity of the above program is O(pow2). It is because the first recursive call takes O(pow) time, the second recursive call takes O(pow - 1) time, the third recursive call take O(pow - 2) time. Adding all of them leads to the asymptotic time complexity of O(pow2).

## Optimization:

The above program can be optimized further to reduce the time complexity to O(pow).

FileName: ComputeSum2.java

Output:

```The sum of digits of numbers from 1 to 17 is: 81
The sum of digits of numbers from 1 to 328 is: 3241
The sum of digits of numbers from 1 to 5 is: 15
```

## Help Others, Please Share   