In the 1970 MAA High School Mathematics Contest (Problem #31), students faced this question:
If a number is selected at random from the set of all five-digit numbers in which the sum of the digits is equal to 43, what is the probability that this number will be divisible by 11?
In base 10, the maximum possible digit is 9, so five digits can sum to at most:
$$9 \times 5 = 45$$
A digit sum of 43 is 2 less than 45. There are only two ways this can occur:
1. One digit is 7, and the other four are 9 (e.g. 79999, 97999, 99799, 99979, 99997).
2. Two digits are 8, and the other three are 9 (e.g. 88999, 89899, ...).
Instead of writing down all combinations and testing divisibility by hand, we can write a for-loop in Lua that checks every 5-digit number ($10000$ to $99999$), filters the ones whose digits sum to 43, and calculates the exact probability!
d_sum = d1 + d2 + d3 + d4 + d5 if d_sum == 43 then total = total + 1 if num % 11 == 0 then div11 = div11 + 1 end end
Move the mouse over a dotted box for more information.
The probability of selecting a number divisible by 11 from this set is:
$$\text{Probability} = \frac{\text{Numbers divisible by 11}}{\text{Total numbers with digit sum 43}}$$
Now you try.
Set digit_sum = d1 + d2 + d3 + d4 + d5 and check divisibility with num % 11 == 0. Run the code to find the total count and the exact probability!
Type your code here:
See your results here:
This code contains ???? in two spots: where digit_sum is computed from the 5 digits, and inside the inner if condition checking if num is divisible by 11. Complete these two lines!
Share your code
Show a friend, family member, or teacher what you've done!