Champernowne word
Up for the count
def counting_series(n):
The Champernowne word 1234567891011121314151617181920212223... is an infinitely long string of digits made up of all positive integers written in ascending order without any separators between the individual numbers. This function should return the integer digit that lies in the position n of the Champernowne word, position count again starting from zero as usual.
Of course, the automated tester will give your function values of n large enough that anybody trying to solve this problem by explicitly constructing the series as a string would run out of time and space long before receiving the answer. Instead, you should observe that the structure of this infinite sequence is quite straightforward, as it starts with 9 single-digit numbers, followed by 90 two-digit numbers, followed by 900 three-digit numbers, and so on. Such a predictably self-similar structure allows you to skip over prefixes of this series in exponentially widening leaps and bounds, until you reach the position n and find out the digit that is waiting for you there.
n | Expected result |
0 | 1 |
100 | 5 |
10000 | 7 |
10**100 | 6 |
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images