All odd numbers from 51 to 5001 are written. What is total number of digits used?
Just noticed a discrepancy in the title and the body. Based on the title: There are
18,806 total digits
From 51-99 there are 49 2 digit numbers. Then 900 3 digit numbers, then from 1,000-5,001 there are 4,002 4 digit numbers. Multiply together 49 x 2 + 900 x 3 + 4,002 x 4 to get the total count.
Based on the body (odd numbers only) it becomes:
25 x 2 + 450 x 3 + 2,001 x 4 = 9,404 digits
Use a python code:
print(len(''.join([str(i) for i in range(51, 5002) if i%2])))
Let me break it down:
If I do
numbers = [i for i in range(1, 10)], python will interpret it as
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]. The last number is omitted, hence in my code I used
Python won't let us count the digits of numbers in a practical way, so I convert them into strings with
if i % 2means if the number divided by two is not zero.
Now we have a list of all the odd numbers in strings, time to find the length. Instead of summing up the length of each number, we can connect all the strings into one large string, and then find the length of that single string.
''.join()does the trick.
Finally, I use
len()to find the length of that large string and print the result.
Looping over each and every number might not be practical, but hey, it's easier to understand, right?