Finding nth digit of natural numbers sequence in JavaScript

In this problem, we need to find the nth digit in the infinite sequence formed by concatenating natural numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12...

When we remove commas and spaces, this becomes: "123456789101112..." and we need to find the digit at a specific position.

Understanding the Problem

The natural number sequence when concatenated forms:

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

For position 13, the digit is 1 (from number 11).

Simple Approach: String Concatenation

The straightforward method builds the sequence string until we reach the desired position:

const num = 13;

const findDigit = (num = 1) => {
    let str = '';
    let i = 1;
    
    while (str.length < num) {
        str += i;
        i++;
    }
    
    const required = str[num - 1];
    return required;
};

console.log(findDigit(num));
console.log("Sequence: " + "123456789101112".substring(0, 15));
1
Sequence: 123456789101112

Optimized Mathematical Approach

For large positions, building the entire string is inefficient. We can calculate mathematically which number contains the nth digit:

const findDigitOptimized = (n) => {
    let digits = 1;  // Current digit count
    let count = 9;   // Numbers with 'digits' digits
    let start = 1;   // First number with 'digits' digits
    
    // Find which "group" (1-digit, 2-digit, etc.) contains our position
    while (n > digits * count) {
        n -= digits * count;
        digits++;
        count *= 10;
        start *= 10;
    }
    
    // Find the exact number and digit position within that number
    const number = start + Math.floor((n - 1) / digits);
    const digitIndex = (n - 1) % digits;
    
    return number.toString()[digitIndex];
};

console.log("Position 13:", findDigitOptimized(13));
console.log("Position 100:", findDigitOptimized(100));
console.log("Position 1000:", findDigitOptimized(1000));
Position 13: 1
Position 100: 5
Position 1000: 3

How the Optimization Works

The optimized approach divides numbers into groups:

  • 1-digit numbers (1-9): 9 numbers × 1 digit = 9 positions
  • 2-digit numbers (10-99): 90 numbers × 2 digits = 180 positions
  • 3-digit numbers (100-999): 900 numbers × 3 digits = 2700 positions

Comparison of Methods

Method Time Complexity Space Complexity Best For
String Concatenation O(n) O(n) Small positions
Mathematical O(log n) O(1) Large positions

Testing Multiple Cases

const testCases = [1, 9, 10, 11, 13, 15, 20];

testCases.forEach(pos => {
    console.log(`Position ${pos}: ${findDigitOptimized(pos)}`);
});
Position 1: 1
Position 9: 9
Position 10: 1
Position 11: 0
Position 13: 1
Position 15: 2
Position 20: 1

Conclusion

Use string concatenation for small positions and the mathematical approach for efficiency with large positions. The mathematical method avoids memory issues and provides O(log n) performance.

Updated on: 2026-03-15T23:19:00+05:30

681 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements