Log in Sign up
Back to Discover
🔢

Modulo

math Maturity 7-9

Sometimes things do not fit evenly.

Divmod truncated.svg
Divmod truncated.svg
Imagine you have five cookies. You want to share them with one friend. You each get two. One cookie is left over. That leftover is the extra bit. Can you find the extra bit?
Divmod floored.svg
Divmod floored.svg
It is fun to count the leftovers!

51 words

Sometimes things do not fit evenly.

Divmod truncated.svg
Divmod truncated.svg
Imagine you have five cookies. You share them with one friend. You each get two. One cookie is left over. This leftover is called the remainder.
Divmod Euclidean.svg
Divmod Euclidean.svg
In math, we call this the modulo. It is a way to find the extra bit. If you have nine cookies and share them with three friends, there is no extra bit. The remainder is zero. Modulo helps us find what is left over when we divide.

83 words

Imagine you are dividing things into equal groups. Sometimes, a few items are left over. In math, we call this leftover part the remainder. The modulo is a special way to find that remainder.

Divmod truncated.svg
Divmod truncated.svg
For example, if you have five items and divide them by two, you get a remainder of one. We can write this as "5 mod 2 = 1". If you have nine items and divide them by three, the remainder is zero.
Divmod Euclidean.svg
Divmod Euclidean.svg
Computers use modulo a lot. They use it to solve puzzles and keep data safe. Many computer languages use symbols like "%" or the word "mod" for this.
Divmod floored.svg
Divmod floored.svg
Different computers may handle negative numbers in different ways. Some keep the remainder positive. Others let the remainder be negative. This can lead to mistakes if a coder is not careful. One way to check if a number is odd is to see if the remainder is not zero.
Divmod rounding.svg
Divmod rounding.svg
This is because a remainder of zero always means the number is even. Modulo is a very useful tool in the world of math and coding.

187 words

Imagine you are splitting a pile of snacks into equal groups. Sometimes, you will have a few pieces left over that do not fit. In mathematics, this leftover amount is called the remainder. The modulo operation is a special way to find that remainder after division.

Divmod truncated.svg
Divmod truncated.svg
When you divide a number by another, the second number is called the modulus. For example, if you have five items and divide them by two, the remainder is one. We can write this as "5 mod 2 = 1". If you have nine items and divide them by three, the remainder is zero.
Divmod Euclidean.svg
Divmod Euclidean.svg
This simple idea helps us understand how numbers fit together.

There are different ways to calculate the modulo, especially with negative numbers. In math, people often use the Euclidean division method. This method usually chooses the smallest non-negative integer as the remainder.

Divmod floored.svg
Divmod floored.svg
However, computers and calculators sometimes use different rules. Some systems use a method called truncated division. In this version, the sign of the remainder might match the number being divided. This can lead to surprises for people writing computer code. For example, checking if a number is odd by looking for a remainder of one might fail if the number is negative. A safer way is to check if the remainder is simply not zero.

Different programming languages choose different rules for their modulo operations. For instance, the languages Standard Pascal and ALGOL 68 always give a positive remainder. Other languages, like C90, let the specific computer system decide how to handle negative numbers.

Divmod rounding.svg
Divmod rounding.svg
This variety means that a coder must know which rule their language follows. Some languages like Python use a "floored" definition. This means the result depends on how the division is rounded. Because of these different rules, the same math problem might give different answers on different machines. Understanding these rules is a key part of computer science.

Computers use special tricks to make modulo math happen very quickly. Doing a full division every time can be slow for a machine. For certain numbers, like powers of two, computers can use a faster method called a bitwise AND operation.

Divmod Euclidean.svg
Divmod Euclidean.svg
This is a way for the hardware to look at the tiny pieces of a number to find the remainder. Compilers, which are tools that help run code, can often spot these patterns. They can automatically change a slow modulo command into a fast bitwise command. This helps programs run smoothly without the person writing the code having to do extra work. It is a clever way to save time and energy.

Modulo is also a very important tool in the world of secret codes. In a field called cryptography, math properties of the modulo are used to keep data safe. For example, the Diffie-Hellman key exchange uses these properties to help people share secrets.

Divmod floored.svg
Divmod floored.svg
You can also use a "modulo with offset" to change the range of the results. Instead of the remainder being between zero and the modulus, it can sit between two other numbers. This is useful for many different types of math problems. Whether it is for keeping secrets or making software fast, the modulo is a powerful idea. It turns the simple idea of leftovers into a major part of our digital world.

555 words

The modulo operation is a mathematical process that identifies the remainder after one number is divided by another. In this operation, the number being divided is called the dividend, and the number used to divide it is known as the modulus. While division focuses on how many times a number fits into another, modulo focuses specifically on what is left over. This concept is vital in both pure mathematics and computer science. It allows us to manage cycles, handle repeating patterns, and secure digital communications.

Divmod truncated.svg
Divmod truncated.svg

To understand the mechanism, consider the Euclidean division of a dividend by a modulus. When you perform this division, you obtain a quotient and a remainder. The modulo operation simply extracts that remainder. For example, if you calculate 5 mod 2, the quotient is 2 and the remainder is 1. Therefore, 5 mod 2 equals 1. If you divide 9 by 3, the quotient is 3 and the remainder is 0, so 9 mod 3 equals 0. For integer operations, the resulting value always falls within a specific range from 0 to one less than the modulus.

Divmod Euclidean.svg
Divmod Euclidean.svg

There are several distinct types of modulo definitions used depending on the context. In mathematics, the result is often viewed as an equivalence class. The most common representative for this class is the least positive residue, which is the smallest non-negative integer in that group. However, computing systems often use different conventions. Truncated division is common in many programming languages, where the remainder takes the sign of the dividend. Floored division, used in languages like Python, rounds the quotient toward negative infinity. Euclidean division is another variant that always ensures a non-negative remainder.

Divmod floored.svg
Divmod floored.svg

History and the development of these definitions are tied to how different hardware and languages were built. For instance, the C90 language leaves the result of a modulo operation involving negative numbers up to the specific implementation. In contrast, Standard Pascal and ALGOL 68 are designed to always provide a positive remainder or zero, even if the divisor is negative. These different approaches mean that a programmer must be very careful when working with negative integers. A common mistake occurs when testing if a number is odd. In a truncated system, a negative odd number like -3 mod 2 returns -1 instead of 1, which can cause logic errors.

Divmod rounding.svg
Divmod rounding.svg

Significant performance considerations exist for implementing these operations in hardware. Calculating a full division can be computationally expensive for a processor. For special cases, such as when the modulus is a power of two, computers can use a faster bitwise AND operation. This is expressed as x % 2^n == x & (2^n - 1). Many modern compilers perform this optimization automatically. They recognize the pattern and swap the slow division for the fast bitwise operation. This allows programmers to write clear, readable code without losing execution speed.

Divmod ceiling.svg
Divmod ceiling.svg

Modulo arithmetic also possesses unique mathematical properties that make it useful for complex tasks. It can be factored or expanded using identities involving multiplication and exponentiation. These properties are essential in the field of cryptography. For example, the Diffie-Hellman key exchange relies on these modular properties to allow two parties to share secret keys securely. One important rule is Fermat's little theorem, which states that if p is a prime number and does not divide a, then a^(p-1) mod p is 1. Such identities allow for the creation of highly secure encryption systems.

Divmod Euclidean.svg
Divmod Euclidean.svg

Beyond standard division, mathematicians use a concept called modulo with an offset. This allows the result of the operation to fall within a specific range, such as between a chosen number and that number plus the modulus. This is useful when a calculation needs to stay within certain bounds. The standard modulo operation is simply a special case of this, where the offset is zero. Whether it is being used to optimize a computer chip or to protect a private message, the modulo operation is a fundamental tool in the modern world.

669 words
🖼️ Images & Media (5)
File:Divmod truncated.svg
Divmod truncated.svg
File:Divmod floored.svg
Divmod floored.svg
File:Divmod Euclidean.svg
Divmod Euclidean.svg
File:Divmod rounding.svg
Divmod rounding.svg
File:Divmod ceiling.svg
Divmod ceiling.svg
Up Next
🔢
Division (mathematics)
Math
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.