For the integers between 0 and 100. How many have the digit 7 in them?
Here is another great question for when you a doing a job interview.
Question: For the integers between 0 and 100 (i.e. 1,2,3,4 etc.) – How many have the digit 7 in them?
Can you figure it out?
The answer can be found in the first comment.
Answer: 19
You have 7, 17, 17 … 97 and also 70, 71 … 79.
The most common answer seems to be 10 and then second most common answer is 11. Some people also say 20, because they count 77 twice.
My thinking was as follows:
If you treat each number as a series of Hundreds, Units and Tens you would have a 100 x 3 grid
001
002
003
.
.
.
099
100
You can discount the hundreds as that will be either 0 or 1, never 7. After that you can see that each column would have 7 ten times each; with one row that both the tens and the units would be 7.
Personally I find it very helpful thinking of sets of numbers with their leading zeros (or right aligned if you prefer).
Those 70s are tricky.
int count = Enumerable
.Range(0, 101)
.Where(n => n.ToString().Contains(”7″))
.Count();
Console.WriteLine(count);