There are four well-known basic arithmetic operations: addition, subtraction, multiplication, and division. The last one is probably the most confusing for most people. In this lesson, we'll refresh your knowledge about division and also talk about its "modification" for positive numbers – modulo division.
Basic division and division with remainder
Let's look at the two examples below and try to explain the difference between them:
Attentive readers may notice that in the left equation above the result of the division is an integer when in the right one it is not a natural number. That happens because is a multiple of , while is not. Such examples are called the non-integer division (in programming languages, it is usually known as float division), despite the result could be either integer or real. At an elementary level, the division result (speaking about natural numbers) is the number of times one number is contained within another one. In the previous example, we can see that number six contains number two exactly three times. The fractional part in the second case occurs because number could not be represented as an exact sum of twos:
The last representation of number illustrates that number is fully contained in it three times. The last summand is called the remainder. It illustrates what remains if the maximum possible multiple of the divisor would be subtracted from the original dividend. In case when the dividend is a multiple of the divisor (the quotient is integer), the remainder equals . The remainder is always a non-negative integer, which is strictly less than the divisor.
Two operators which return an integer part of the division and its remainder are and respectively. Let's demonstrate how they work using an example with :
Note: and operators are identical to the integer part of quotient and remainder relatively only for positive operands. If one of the operands is negative, and operators work in another way. This is beyond the scope of this topic, but interested readers can find more information by themselves. That is why we will work with positive numbers only.
Modulo division
So let's go into details of how the operator works.
Assume that we have a number , and . Then
(read as "x modulo y is equal to z")
In case when :
With the assistance of a operator, we could check whether one number is divisible by another:
is divisible by if and only if
i.e. x modulo y is equal to 0.
In the majority of programming languages the operator is written as %, i.e. would be written as % .
Examples
Now let's consider several cases to grasp what we have learned.
1. Even or not
For instance, you have a task to check if the number is even or not. It is extremely simple using the operator. You know that a number is even if it is divisible by . So, a given number is even if and only if modulo equals , i.e.:
And this means that in your code you just need to check if the condition % == is met.
2. What time is it now?
As you know, in the world there are two widespread time conventions: -hour clock and -hour clock. Imagine that you have a friend who uses the -hour clock and you ask him what time is it now in his city. He answered you, but, unfortunately, you live according to the -hour one and don't understand what time is there exactly. However, this problem could be solved with the program code and the assistance of the operator.
Assume that your friend said that it is o'clock at the moment. How to transfer it to the -hour clock? No problem! Just think, what is a -hour clock: people who use it just consider the full day as two full turns of the clock. Same as you, but instead of using a.m. or p.m. they numerate times continuously from to . And the secret of conversion is taking time in -hour format modulo .
Your friend said that it was o'clock at the moment. Let's convert it to -hour format:
because
And since , o'clock is equal to p.m.
Is it useful?
The functionality of the mod operator is very simple when you understand it. However, if it is the first time you faced it, some troubles could arise. And the reasonable question is "Is it really useful?". The answer is "Absolutely, yes!".
One of the most significant examples of usage of the mod operator is cryptography. In this area, programmers need to have a function, which is difficult to calculate the original value from, knowing its result. For instance, hackers have known that result of modulo is equal to . And they would like to know what is . It is a very hard task, because any number, that looks like , where is an arbitrary natural number, gives you . There are millions of possible values of . Sure, encryption algorithms are much more complicated than just taking the remainder from the division. But the idea of using it is very important.
It is also used when you need to operate with extremely big numbers and your resources are restricted. Applying mathematical basic operators (, , , ) not to the big number itself, but to its remainder makes your computation thousands of times easier and allows you to make conclusions faster. It is not possible in every case, but sometimes it helps a lot.