← Back to challenges

Sum the Digits of All Integers 0 to 10^n - 1

JavaScriptHardmathnumbersalgebra

Instructions

Create a function that takes an integer parameter, n, and returns the sum of all the digits in each integer in the range 0 to 10^n - 1 inclusive.

  • If n is 1, the range is 0 to 9.
  • if n is 2, the range is 0 to 99.
  • if n is 12, the range is 0 to 999999999999.

n will always be >= 1.

Because of JavaScript's representation of numbers, n will be limited to 125

Examples

sumDigitsInRange(1) ➞ 45

sumDigitsInRange(2) ➞ 900

sumDigitsInRange(3) ➞ 13500

sumDigitsInRange(8) ➞ 3600000000

sumDigitsInRange(13) ➞ 585000000000000

Notes

The function should run quickly.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.