# What is the probability that the digit sum of a randomly chosen integer between 0000 and 9999 is divisible by 5?

If I have a randomly selected integer between 0000 and 9999, what is the probability that the digit sum of that number is divisible by 5? [E.g. 1234 = 1 + 2 + 3 + 4 = 10]

I've started off with knowing that I have 2 options for the last integer, but I'm not sure where to go from there.

• I suggest you try the problem first for 1-, 2-, and 3-digit numbers. – MJD Aug 23 '17 at 12:34
• You seem to be thinking about a different problem, namely what is the probability that a number between $0000$ and $9999$ is divisible by $5$. – N. F. Taussig Aug 23 '17 at 12:38
• Yeah, that's what I thought too. What I said doesn't sound right. – Broadsword93 Aug 23 '17 at 12:42
• @N.F.Taussig The logic is just as good for the stated problem once the first three digits are fixed! – A Simmons Aug 23 '17 at 16:37
• what you mean bei "sum" when talking about a single integer, that confuses me. – Zaibis Aug 24 '17 at 9:29

Hint: Pick the first three digits randomly first, and then focus on the last one.

It's similar to how the probability of getting the sum $7$ when throwing two dice can be seen to be $\frac16$ by noting that no matter what the first die shows, the result on the second die can make the sum $7$ in exactly one way.

• I've thought of doing something like 10 x 10 x 10 x 2, but I'm not sure if it's correct. – Broadsword93 Aug 23 '17 at 12:36
• @Broadsword93 Arthur is saying that you choose randomly any digit for first three places. This is done in $10^3$ ways. Now, the sum of these digits can be of form $5k+r$ where $0\le r < 5$. So make $5$ cases for the sum being $5k+r$. Now take suitable values for units digit. Eg. Say we choose 1,3,5. Now this sum is 9, or $5\times 1 + 4$. So units digit can be 1 or 6. Similarly there will be 2 choices for units digit in all cases. So answer should be $10^3\times 2$ – samjoe Aug 23 '17 at 12:45
• @Broadsword93 so yes, it is correct. – Omnomnomnom Aug 23 '17 at 12:45
• @Broadsword93 That's if you want to count all the possible ways and find the probability as $\frac{2000}{10000}$. I like to think of it like this: Whatever the first three digits are, it's a $2$ in $10$ chance that the final digit will be one that makes the digit sum divisible by $5$. – Arthur Aug 23 '17 at 12:52
• @thumbtackthief Consider the number in base b. No matter what the first few digits are, it is only the last one that determines the digital sum (modulo b). There are b possibilities and only one can cause the final sum (modulo the base) to be 0. Therefore, the probability is 1/b (less the slight imbalance in this case because 10000 isn't divisible by 3 and 11) – Phylogenesis Aug 24 '17 at 8:21

20%, or 1 in 5. I just counted them all in Excel. Consider that in each decade there will be 2 numbers divisible by 5. The first being 0000 and 0005. Then 0014 and 0019. And so on until 0091 and 0096. Then each century will be similar to the first except for a shift like we get with each decade, 0104 & 0109... 0190 & 0195. Likewise with the millennia. Consequently, the odds remain the same, 1 in 5.

• The good thing about maths is that you do not need to count them in excel to know the solution ;) – Ander Biguri Aug 23 '17 at 15:09
• $0$ is divisible by $5$? – user121330 Aug 23 '17 at 16:49
• @user121330: Yes, it's divisible by every nonzero integer, and even divisible by zero depending on what definition of "divisible" you're using. – user2357112 Aug 23 '17 at 17:01
• (You certainly can't perform the division $0/0$, but it's common to define "$a$ is divisible by $b$" as "there exists an integer $k$ such that $kb=a$", with no requirement that any of the integers involved be nonzero.) – user2357112 Aug 23 '17 at 17:05

Take the sum of the first 3 digits, and divide it by 5. You have either 0, 1, 2, 3, 4, with probabilities p_0, p_1, p_2, p_3, p_4. You could likely prove that p_i = 0.2 for all i, but there is no need. All you need is that sum(p_i) = 1

for any given i, there are two numbers that the last digit could be. For example, if i = 1, the last digit could be 4 or 9. Since 2 digits out of 10 possibles is 20%, for any given sum of the first 3 digits, there is a 20% chance that the total sum is divisible by 5

So you have: 0.2*p_0 + 0.2*p_1 + 0.2*p_2 + 0.2*p_3 + 0.2*p_4 = 0.2*(p_0+p_1+p_2+p_3+p_4) = 0.2*1 = 0.2

so 20% chance

In this case we had it easy since the base 10 is a multiple of the divisor 5. What happens if we have $n$ digits in base $B$ and are interested in divisor $d$?

Let $\omega \neq 1$ be a $d$th root of unity, and consider $$P(\omega) = \left(\frac{1 + \omega + \omega^2 + \cdots + \omega^{B-1}}{B}\right)^n = \left(\frac{1-\omega^B}{B(1-\omega)}\right)^n.$$ The coefficient of $\omega^r$ is the probability that the remainder is $r$. We can extract the probability of a zero remainder by going over all roots of unity (using $P(1) = 1$): $$\Pr[r=0] = \frac{1}{d} + \frac{1}{d} \sum_{t=1}^{d-1} \left(\frac{1-\omega^{tB}}{B(1-\omega^t)}\right)^n.$$ It is not immediately clear why this expression is real. The reason is that the terms for $t$ and $d-t$ are complex conjugates (since $\omega^{d-t} = \overline{\omega^t}$).

Perron-Frobenius theory tells us that the norm of the eigenvalues $\frac{1-\omega^{tB}}{B(1-\omega^t)}$ is strictly less than 1, and so the convergence to $1/d$ is exponentially fast.

This is badly written I admit, as I did this quickly. But this script goes through all the numbers from 0 to 9999 and works out the percentage that have a digit sum that is divisible by 5. As you can see, it is bang on 20%. Hope this helps! (written using python)

count = 0
for k in range(10000):
a = str(k)
digitsum = 0
for b in a:
num = int(b)
digitsum += num
if not digitsum%5: # If its divisible by 5
count += 1

print('Probability of digitsum being divisible by 5 = %s%%'%((count/10000)*100))


(0 is taken to be divisible by 5 here. If you don't want to include it, your probability will be slightly less than 20%)

Javi is almost right! Why focus on the last digit? For example the digit sum of 104 is divisible by 5. So we have to check the possiblities. The smallest sum is zero and the biggest possible sum having four digits is four times nine = 36, as Javi just explained correctly. Now let's look how many of the possible digit sums are divisible by 5: 0, 5, 10, 15, 20, 25, 30 and finally 35. So one could assume that P is 8/36 $\approx$ 0,22. But thats not true... Looking at the digit sums for each decade one will see that there are always only two sums divisable by 5. So the value of 0,2 or 20 % is correct.

• There are many more ways to pick 4 digits that add up to 20 than there are ways to get them to add up to 36. The distribution is not uniform and your conclusion is wrong. Would you say the chances of rolling a 7 with two dice is 1/11 because every number between 2 and 12 can be the sum? – Phylogenesis Aug 24 '17 at 8:09
• because focusing on the last digit gives us the right answer 1/5, not 7/36. You can't calculate chances so easily in your case coz same sum repeats several times and number of repetitions depends on the value of the sum, like 1: 0001,0010,0100,1000, but 2:0002,...,0101,so on. Or, in precise terms, the sums aren't uniformly distributed (as it stated in comments on Javi's answer). – dk14 Aug 24 '17 at 8:09