A number is picked at random from 1 to 50. What is the probability that it is divisible by 3 or 5?
23/50. Multiples of 3: 16. Multiples of 5: 10. Multiples of both (15, 30, 45): 3. By inclusion-exclusion, 16 + 10 - 3 = 23 favourable numbers out of 50.
Explain it simply
When you count "A or B", the items that are in both get counted twice if you just add. Numbers like 15 and 30 are multiples of both 3 and 5, so subtract them once. This rule, P(A or B) = P(A) + P(B) - P(A and B), is called inclusion-exclusion.
Example
Question: Random number from 1 to 50. P(divisible by 3 or 5)?
- Multiples of 3: floor(50/3) = 16.
- Multiples of 5: floor(50/5) = 10.
- Multiples of 15: floor(50/15) = 3.
- Favourable = 16 + 10 - 3 = 23.
- P = 23/50.
Related: P(divisible by neither) = 1 - 23/50 = 27/50.
let n = 0;
for (let i = 1; i <= 50; i++) if (i % 3 === 0 || i % 5 === 0) n++;
console.log(n); // Output: 23What to say in the interview
"I count multiples of 3 and of 5, then subtract the multiples of 15 which got counted twice. 16 plus 10 minus 3 is 23, so 23 over 50."
Common mistakes
- Adding 16 + 10 = 26 and forgetting the overlap.
- Counting multiples of 15 as 50/15 = 3.33 instead of the whole number 3.