Blog on Applied Mathematics

Cookies, clocks and soldiers: modular arithmetic!

Introduction and the 7 dwarfs

It was October when one of my professors entered in the lesson room and said: “Once upon a time, Snow White wished to prepare cookies for the seven dwarfs. Obviously…with some conditions. She wanted to prepare the least number of cookies such that dividing them into 2 dwarfs there would remain only one, dividing them into 3 dwarfs there would remain only one and so on for 4,5 and 6 dwarfs but…dividing them into all the 7 dwarfs, there would not remain any cookie.” This problem generated, of course, hilarity and some puzzled faces appeared in the room because of the nonsense of the story and because of the absurdity of the question. It was one of the first lessons during my first year at University and I honestly remained astonished.


Since for the moment it is not very important, we’re not going to talk of Snow White’s OCD but we will focus on the mathematical side of the story. What the professor wants is the smaller positive integer number (a number of the set 0,1,2,…) such that, if you divide it by 2,3,4,5 and 6, you get 1 as remainder and, if you divide it by 7, the remainder is 0.

It seems to be only an hard calculation and of course it is! But, behind this story, there’s some beautiful maths waiting to be discovered. It is called Modular Arithmetic. The good thing is that everyone can understand it…well…maybe it is better to say that everyone already knows it! The answer is in the clock.

The clock

Everyone knows what a clock is. If now it’s 9 a.m. and someone tells that you have a meeting in 2 hours, you’ll immediately understand that your meeting is at 11 a.m.. That’s right! And the reason si that 9+2=11. But, if someone tells that your meeting is in 4 hours, you’ll immediately understand that your meeting is at 1 p.m.. However, 9+4=13, not 1. This seems funny but it’s not!

What are we doing? We are restricting our number set! We are not deleting useless numbers but only collecting them into groups of numbers. So, some of them will coincide, as 13 and 1 for example. This relation is called congruence.

So, for examples with small numbers it’s easy, but for ones with big numbers? If now it’s 5, in 1000000 hours, what time is it?

Here’s the problem…this is not so intuitive…so? No problem, Maths is coming to save us!

We can easily identify 1 with 13, 2 with 14, 3 with 15 and so on. But what’s the relationship? Simple, if you divide 14 by 12, the remainder is 2…if you divide 15 by 12, the remainder is 3…and so on. So we identify all the numbers with the same remainder in the division by 12. So the question “If now it’s 5, in 100000 hours, what time will it be?” now has an answer!

We consider 1000000 and we add 5. We obtain 1000005 that is 9+12*83333. So the remainder of the division by 12 is 9. It will be 9.

Snow White’s clock

Now, let’s imagine that there exist clock with an arbitrary number of hours. Let n be this number. In this strange world with n-hour-clocks making calculations is different but not so much.

If it’s 0 now, after x hours, what time will it be? We should divide by n and look at the remainder. This will tell what time it is. Amazing, isn’t it?

Let us now give names to things. The numbers a and b are congruent modulo n if the remainders of the divisions by n are the same. I write a \cong b \; (\mod n).

So, two numbers a and b are congruent modulo n if, starting from the 0, after a or b hours, the question “what time will it be?” has the same answer.

This kind of Arithmetic is useful in many cases. It has plenty of applications in pure mathematics (primality tests, Chinese Remainder Theorem, divisibility rules,…) and in applied mathematics (cryptography).

I’m not going to talk about these things now, but I’ll do it in the future!

Chinese soldiers

You now have this brand-new formalism and you can reformulate Snow White’s problem. She wants a number m that is congruent to 1 modulo 2,3,4,5 and 6 and that is congruent to 0 modulo 7. Easier, isn’t it?

But…we haven’t solved it yet…to do this, let’s come back to an ancient past.

Long long time ago, many years before Christ, Chinese people knew about Modular Arithmetic! They used it to count people, especially soldiers!

I’m not talking of small numbers of soldiers but of a land full of people. So, how can you count them?

There’s a smart way to do it. You decide a priori a set of numbers called n_1, n_2, \dots, n_p. Then you ask to soldiers to divide in groups of n_1 people and you report the remaining number of them (i.e. the remainder). Let us call it a_1. You see that the number x of soldiers is congruent to a_1 modulo n_1.

You do the same with the other numbers and, finally, you obtain the system

x \cong a_1 (\mod n_1)

x \cong a_2 (\mod n_2)


x \cong a_p (\mod n_p).

Essentially it’s the same thing Snow White needs to do!

Let’s do a simple example just to explain how it works. Let suppose that the number of soldiers satisfies the simple system:

x \cong 1 (\mod 2)

x \cong 2 (\mod 3)

From the first equation I see that x=2N+1, where N is an integer. That’s true because if I divide x by 2, the remainder is 1. Moreover, these are all the numbers with this property.

We substitute x in the second one obtaining 2N+1 \cong 2 \mod 3, that is 2N\cong 1.

Multiplying by 2 both sides, we get N\cong 4N\cong 2 because 4 is congruent to 1 modulo 3. Hence, N=3M+2 for M integer. Substituting again we get x=6M+5. So, x can be a number in the set of the number that are congruent to 5 modulo 6.

The solution to this problem is not unique in principle. Anyway, knowing the upper and lower bounds of the searched number, we can obtain the exact number. For example if someone told us that x lies between 23 and 30, we would have understood that x is 25. This is how experts suppose Chinese people did.


Obviously, I made it simpler than how it is in its fullness. I omitted some details that the reader can search by himself if he/she got curious after reading this post. I suggest to give a look to the Chinese Remainder Theorem, it’s very interesting.

Coming back to Snow White, the number of cookies she needs to prepare must be a number that is congruent to 301 modulo 420. Since she wanted the smaller one, the answer is 301. And…if you don’t believe it…divide and see!

No Comments Yet

Leave a Reply

Your email address will not be published. Required fields are marked *