All the base ten positive integers are written successively, without commas or spaces, resulting in this infinite string.
1234567891011121314......
Reading left to right, determine the digit following and the digit preceding the 2011th incidence of the digit 1. What is the digit following and the digit preceding the 20110th incidence of the digit 1?
CLS
FOR n = 1 TO 9999
prev$ = curr$
curr$ = LTRIM$(STR$(n))
curLen = LEN(curr$)
totLen = totLen + curLen
cur1s = 0
FOR i = 1 TO LEN(curr$)
IF MID$(curr$, i, 1) = "1" THEN
cur1s = cur1s + 1
tot1s = tot1s + 1
IF tot1s = 2011 THEN PRINT curr$, i
END IF
NEXT
NEXT
PRINT tot1s
finds
3172 2
which means that the 2011th 1 is the 1 in 3172, and so is bounded by a 7 on the right and a 3 on the left.
A modified version of the program
CLS
FOR n = 1 TO 99999
prev$ = curr$
curr$ = LTRIM$(STR$(n))
curLen = LEN(curr$)
totLen = totLen + curLen
cur1s = 0
FOR i = 1 TO LEN(curr$)
IF MID$(curr$, i, 1) = "1" THEN
cur1s = cur1s + 1
tot1s = tot1s + 1
IF tot1s = 20110 THEN PRINT curr$, i
END IF
NEXT
NEXT
PRINT tot1s
finds
23517 4
indicating the answer to the second part is that the digit 7 follows the 20110th 1 and the digit 5 precedes it.
Incidentally, it also found the total number of 1's between 1 and 9999 is 4000 and those between 1 and 99999 number 50000. When the upper limit is 1 less than a power of 10, this total number is log(L+1)*(L+1)/10, where L is that limit.

Posted by Charlie
on 20110518 17:54:40 