Overview
Given an integer n find the nth digit in the infinite sequence {1, 2, 3, 4 ….. infinity}
Example
Input: 14
Output: 1
The 14th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 … is a 1, which is part of the number 12.
Example 2
Input: 17
Output: 3
The 17th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 … is a 3, which is part of the number 13.
Program
Here is the program for the same.
package main
import "fmt"
func findNthDigit(n int) int {
numDigits := 1
tenIncrement := 1
start := 1
counter := 9 * tenIncrement * numDigits
for n > counter {
n = n - counter
tenIncrement = tenIncrement * 10
numDigits++
start = start * 10
counter = 9 * tenIncrement * numDigits
}
return findNthDigitUtil(start, numDigits, n)
}
func findNthDigitUtil(start, numDigits, n int) int {
position := n % numDigits
digitWhichHasN := 0
if position == 0 {
digitWhichHasN = start - 1 + n/numDigits
return digitWhichHasN % 10
} else {
digitWhichHasN = start + n/numDigits
positionFromBehind := numDigits - position
answer := 0
for positionFromBehind >= 0 {
answer = digitWhichHasN % 10
digitWhichHasN = digitWhichHasN / 10
positionFromBehind--
}
return answer
}
return 0
}
func main() {
output := findNthDigit(14)
fmt.Println(output)
output = findNthDigit(17)
fmt.Println(output)
}
Output
1
3