#1: How many digits are there in the positive integers 1 to 99 inclusive?
Solution I: From 1 to 9, there are 9 digits.From 10 to 99, there are 99 - 10 + 1 or 99 - 9 = 90 two digit numbers. 90 x 2 = 180
Add them up and the answer is 189.
Solution II: ___ There are 9 one digit numbers (from 1 to 9).
___ ___ There are 9 * 10 = 90 two digit numbers (You can't use "0" on the tenth digit but you
can use "0" on the unit digit.) 90 * 2 + 9 = 189
# 2: A book has 145 pages. How many digits are there if you start counting from page 1?
There are 189 digits from page 1 to 99. (See #1, solution I)From 100 to 145, there are 145 - 100 + 1 or 145 - 99 = 46 three digit numbers.
189 + 46*3 = 327 digits.
#3: "A book has N pages, number the usual way, from 1 to N. The total number of digits in the page number is 930. How many pages does the book have"? Similar to one Google interview question.
Read the questions and others here from the Wall Street Journal.
Solution I:
930 - 189 (digits of the first 99 pages) =741741 divided by 3 = 247. Careful since you are counting the three digit numbers from 100 if the book has N
pages N - 100 + 1 or N - 99 = 247. N = 346 pages.
Solution II:
930 - 189 (total digits needed for the first 99 pages) = 741741/3 = 247 (how far the three digit page numbers go).
247 + 99 = 346 pages
#4: If you write consecutive numbers starting with 1, what is the 50th digit you write?
Solution I: 50 - 9 = 41, 9 being the first 9 digits you need to use for the first 9 pages.
Now it's 2 digit. 41/2 = 20.1 , which means you will be able to write 20 two digit numbers + the first digit of the next two digit numbers.
10 to 29 is the first 20 two digit numbers so the next digit 3 is the answer. (first digit of the two digit number 30.)
Solution II: (50 - 9 ) / 2 = 20. 5 ; 20.5 + 9 = 29.5, so 29 pages + the first digit of the next two digit numbers, which is 3, the answer.
#5: What is the sum if you add up all the digits from 1 to 100 inclusive?
00 10 20 30 40 50 60 70 80 90
01 11 21 31 41 51 61 71 81 91
02 12 22 32 42 52 62 72 82 92
03 13 23 33 43 53 63 73 83 93
04 14 24 34 44 54 64 74 84 94
05 15 25 35 45 55 65 75 85 95
06 16 26 36 46 56 66 76 86 96
07 17 27 37 47 57 67 77 87 97
08 18 28 38 48 58 68 78 88 98
09 19 29 39 49 59 69 79 89 99
Solution I:
Do you see the pattern? From 00 to 99 if you just look at the unit digits.
There are 10 sets of ( 1+ 2 + 3 ... + 9) , which gives you the sum of 10 * 45 = 450
How about the tenth digits? There are another 10 sets of (1 + 2 + 3 + ...9) so another 450
Add them up and you have 450 * 2 = 900 digits from 1 to 99 inclusive.
900 + 1 ( for the "1" in the extra number 100) = 901
Solution II:
If you add the digits on each column, you have an arithmetic sequence, which is
45 + 55 + 65 ... + 135 To find the sum, you use average * the terms (how many numbers)
\(\dfrac {45+135} {2} * \left( \dfrac {135-45} {10}+1\right)\) =900
900 + 1 = 901
Solution III :
2*45*101 + 1 = 901
Problems to practice: Answers below.
#1: A book has 213 pages, how many digits are there?
#2: A book has 1012 pages, how many digits are there?
#3: If you write down all the digits starting with 1 and in the end there are a: 100, b: 501 and c: 1196 digits, what is the last digit you write down for each question?
#4: What is the sum of all the digits counting from 1 to 123?
Answers:
#1: 531 digits.
#2: 2941 digits.
#3: a. 5, b. 3, c. 3
#4: 1038