# For positive integers between 999 and 100 inclusive, how many contain the digit 5?

The question comes in two parts:

1. For positive integers between 999 and 100 inclusive, how many contain the digit 5 at least once?

2. For positive integers between 999 and 100 inclusive, how many contain the digit 5 exactly once?

Question 1

Numbers between 999 and 100 inclusive:

999 - 99 = 900

Numbers between 999 and 100 inclusive that do not contain digit 5:

8 * 9 * 9 = 648 (The 8 is because the first digit can't be 5 or 0)

Numbers that contain digit 5 at least once:

900 - 648 = 252

Question 2

Total numbers that contain digit 5 exactly once:

(1*9*9) + (8*1*9) + (8*9*1) = 225

I think I got it wrong but not sure which part.

• Well, a good way to check is to count the number with exactly two $5's$. $55X$ gives us $9$, $5X5$ gives us $9$, $X55$ gives us $8$. Thus there are $9+9+8=26$ such numbers. Of course $555$ is the unique number in the range with three $5's$. We check that $252=225+26+1$ which seems like pretty good evidence that you are right.
– lulu
Nov 1 '18 at 16:34
• @DavidG.Stork The OP is counting numbers with no digits equal to $5$ so that they can be subtracted from all three-digit positive integers. Nov 1 '18 at 16:38
• @DavidG.Stork OP is using a standard technique of counting how many numbers there are where we ignore the condition, giving $9\times 10\times 10 =900$ numbers in the range, and subtracting the number of numbers which violate the condition, violating the condition in this case meaning "does not have $5$ as a digit", there being $8\times 9\times 9$ such numbers, which will give the number of numbers that satisfy the condition as a result. Nov 1 '18 at 16:38
• @DavidG.Stork The purpose of that step is to get the number which does not contain digit 5 at all, which in this case is 648. My reasoning is that if I subtract 900 with 648, then all the remaining numbers would contain at least one digit 5. Nov 1 '18 at 16:40
• "I think I got it wrong but not sure which part" Looks good to me. Do you have specific reason to doubt the answer? Be more confident, you seem to be doing well from what I can see. Nov 1 '18 at 16:40

 There is one number with all three digits equal to 5.

There are 1x10x10 = 100 numbers with the first digit equal to 5.
There are 9x1x10 = 90 numbers with the second digit equal to 5.
There are 9x10x1 = 90 numbers with the third digit equal to 5.

There are 10 numbers with the first and second digit equal to 5.
There are 10 numbers with the first and third digit equal to 5.
There are 9 numbers with the second and third digit equal to 5.

There are 8x9x9 = 648 numbers with no digit equal to 5.


This is enough information to fill in the diagram above.

I got the same answers as you with the (javascript) code

var countFives = function(x){
var count = 0;
for (let c of x.toString(10)){
if (c==="5") count++;
}
return count;
};

var s = 0;
for (let i=100; i<=999; i++) if (countFives(i)>=1) s++;
console.log(s);


252

var s = 0;
for (let i=100; i<=999; i++) if (countFives(i)===1) s++;
console.log(s);


225