I am learning python through the Project Euler problems. For problem 40 I wrote this code:
import math
i = 1
counter = 0
while counter <= 1000000:
MMM = int(math.log(i, 10)) + 1
counter = counter + MMM
V = math.log(i, 10)
print(i, counter, MMM, V)
i += 1
It is supposed to return the number containing the Nth digit. Basically, this is supposed to keep track of what would happen if I concatenated the integers from 1 through whatever into another number. The goal is to determine what a specific digit is. This code works below a certain threshold, however, by the time it gets to the millionth digit it is off by one. What am I missing here? I have seen other implementations that save time, but I am more interested in why the count becomes wrong at some point
Edit:
replacing
MMM = int(math.log(i, 10)) + 1
with
MMM = len(str(i))
works like a champ!
Although it would be nice to have an all numeric solution, It'll have to wait until I can trust log functions in Python.