Choose a mode and enter integers. "Remainder" also works with negative numbers and shows how the math remainder (0 or more) differs from the % operator in programming.
Table of Contents
-
What you can do on this page
-
What is this calculation used for?
-
How to Use
-
Formulas and figures
-
Symbols and terms
-
Good to know before you start
-
How to calculate it in Excel
-
How to calculate it in Google Sheets
-
How to calculate it in Python
-
How to write it in LaTeX and other math languages (copy and paste)
-
How to have ChatGPT do the calculation
-
DataChef Features
-
Related Features
-
NumberChef Calculators List
What you can do on this page
- Find the remainder when an integer \(a\) is divided by \(n\). Negative numbers work too (for example, \(-7\) divided by \(3\)): the page shows the math remainder (\(0 \le r < n\)) side by side with the result of the % operator in C, Java, JavaScript and similar languages (which can be negative)
- Check whether the congruence \(a \equiv b \pmod{n}\) holds, with the reason: whether the difference \(a - b\) is a multiple of \(n\)
- Get the exact remainder of a huge power, such as \(7^{100}\) divided by \(13\), with the steps of repeated squaring (numbers too big for a regular calculator are no problem)
- See the remainder on a clock diagram (the circle of mod \(n\)) and get a feel for how remainders go around and around the same positions (the clock appears when the modulus \(n\) is 2 to 24)
- A plain-language explanation of the formulas and copy-and-paste formulas for Excel, Google Sheets and Python are all on this page
What is this calculation used for?
On a clock, "14:00 is 2 p.m." is exactly the remainder calculation 14 mod 12 = 2. Days of the week work the same way, on a clock that goes around once every 7 days. For example, if today is Monday, then since 100 mod 7 = 2, the day 100 days from now is "2 days after Monday", which is Wednesday.
Paper planners and calendar apps alike match dates to days of the week with this remainder calculation.
The last digit of a product barcode (UPC) or a book's ISBN is a check digit made from the other digits with a remainder calculation. For a UPC, for example, the digits are multiplied alternately by 3 and 1 and added up, and the last digit is chosen from the remainder of that total divided by 10.
If one digit is mistyped, the remainder no longer matches, so checkout registers and online stores can catch the mistake on the spot. The check digit in credit card numbers works on the same idea.
RSA, the best-known public-key encryption for sending things like credit card numbers safely, uses "the remainder of a huge power" directly in its calculations. A power mod can be computed quickly, but working backward from the result to the original number is extremely hard. This one-way property is what makes it secure.
Repeated squaring, whose steps the "Power mod" mode on this page shows, is the calculation that encryption software runs every day.
In programming, the % operator is used all the time: i % 2 tells even from odd, and i % n makes an order that loops around every n items (choosing the next player in a turn-based game, going back to the first slide after the last one, and so on).
When negative numbers are involved, % gives different results in different languages, so knowing "the math remainder versus % in C and Java" helps when you hunt for bugs.
Share 63 pencils equally among 12 people, and each gets 5 with 3 left over (63 = 12 × 5 + 3). Packing stock into boxes (how many full boxes of n, and how many left over), splitting people into groups for a party: every "share equally and see what is left" situation is this calculation.
The basic division equation a = n × q + r puts all these everyday situations into one formula.
Formulas and figures
Symbols and terms
Symbols
| \(\equiv\) | is congruent to | The symbol for congruence. It is an equals sign with three lines instead of two, and says "equal in the world of remainders", which is looser than "exactly equal". |
| \(\bmod\) (mod) | mod | Short for "modulus", from the Latin word modulus (a small measure). "\(a \bmod n\)" stands for "the remainder of \(a\) divided by \(n\)", and "\(\pmod{n}\)" written after an equation declares "we are working modulo \(n\)". |
| \(a,\ b\) | a, b | The integers whose remainders you look at. In a congruence, they are the two integers on the left and right. Letters near the start of the alphabet are traditionally used for fixed numbers. |
| \(n\) | n | The modulus (the number you divide by). The letter \(n\), from "number", is often used. On this page it is an integer of 1 or more. |
| \(q\) | q | The quotient, from the first letter of "quotient". When dividing a negative number, the quotient is rounded down to the smaller integer so that the remainder is 0 or more (floor division). |
| \(r\) | r | The remainder, from the first letter of "remainder". In math it is always chosen in the range \(0 \le r < n\). |
| \(m\) | m | The integer in the definition of congruence that tells how many times the modulus goes into the difference. It can be negative or 0. |
| \(a^{k}\) | a to the k | \(a\) multiplied by itself \(k\) times (a power). The small raised number \(k\) is the exponent, which tells how many times to multiply. |
| % | percent (as an operator, mod) | The modulo operator that finds a remainder in many programming languages. It is the same sign as the percent sign but has a different job. In C, Java and JavaScript, its result for negative numbers can differ from the math remainder (see the key idea of formula card 1). |
Terms
| remainder (residue) | What is left over when a division does not come out even. "Residue" is a more formal word for the same thing. In math it is always at least \(0\) and less than the divisor. |
| quotient | The integer that tells how many times the divisor can be taken away. It is the \(q\) in the basic division equation \(a = n \times q + r\). |
| modulus | The number \(n\) you divide by to get the remainders you work with. On a clock dial, it is the number of marks in one full turn. "Modulo 12" is short for "looking at remainders when divided by 12". |
| congruent | Two integers are congruent modulo \(n\) when they have the same remainder when divided by \(n\). This is a term about integers, different from congruent shapes in geometry (same shape and size). |
| congruence | A statement of the form \(a \equiv b \pmod{n}\). You can add, subtract and multiply congruences just like equations, so remainder problems can be worked out by rewriting. It is a basic tool of number theory, used in math contests and computer science. |
| multiple | A number you get by multiplying an integer by another integer. "The difference is a multiple of the modulus" is the definition of congruence. |
| floor division | A way of dividing that rounds the quotient down to the smaller integer. When the divisor is positive, the remainder is 0 or more even for a negative dividend. The // operator in Python and the INT function in Excel work this way. |
| modulo operator | The % operator that finds a remainder in programming. C, Java and JavaScript round the quotient toward 0 (the remainder takes the sign of the dividend). Python and Ruby use floor division (the remainder takes the sign of the divisor, so it is 0 or more when the modulus is positive). |
| repeated squaring | A way to find the remainder of a huge power with few multiplications: square over and over while taking the remainder, split the exponent into a sum of powers of 2 and multiply the pieces. It is also called exponentiation by squaring or square-and-multiply, and is used in implementations of RSA encryption and more. |
| residue class | The idea of grouping integers by their remainder when divided by \(n\). In the world of mod \(n\), every integer falls into one of \(n\) groups, remainders \(0\) to \(n-1\). Even and odd numbers are the grouping by remainder when divided by 2. |
| periodicity | The way the remainders of powers repeat the same pattern. There are only \(n\) possible remainders, so they must come back to one they had before. Finding the cycle in last-digit problems (mod 10) is a classic in math contests. |
| relatively prime | Two integers are relatively prime (or coprime) when their greatest common divisor is 1. It appears in the condition for dividing both sides of a congruence by the same number. |
Good to know before you start
Here is what helps you use the calculation on this page with real understanding, not just by pressing the button.
If you get stuck, going back to these topics is the quickest way forward.
| Division with remainders (Grades 3–4) |
|
| Multiples and factors (Grades 4–6) |
|
| Negative numbers (Grades 6–7) |
|
| Exponents (Grades 6–8) |
|
| The division algorithm for integers (high school) |
|
How to calculate it in Excel
| Dividend a | -7 |
| Modulus (divisor) n | 3 |
| Math remainder (0 or more) | =MOD(B1,B2) |
| Quotient (floor division) | =INT(B1/B2) |
| Check n × quotient + remainder | =B2*B4+B3 |
| C/Java-style remainder (rounded toward 0) | =B1-B2*TRUNC(B1/B2) |
| Integer a | 38 |
| Integer b | 14 |
| Modulus n | 12 |
| Difference a − b | =B1-B2 |
| Remainder of the difference ÷ n | =MOD(B4,B3) |
| Result (TRUE = congruent) | =MOD(B4,B3)=0 |
| Base a | 7 |
| Exponent k | 100 |
| Modulus n | 13 |
| Remainder of a^1 | =MOD(B1,B3) |
| Remainder of a^2 (square the row above, take the remainder) | =MOD(B4^2,$B$3) |
| Remainder of a^4 | =MOD(B5^2,$B$3) |
| Remainder of a^8 | =MOD(B6^2,$B$3) |
| Remainder of a^16 | =MOD(B7^2,$B$3) |
| Remainder of a^32 | =MOD(B8^2,$B$3) |
| Remainder of a^64 | =MOD(B9^2,$B$3) |
| Remainder combined for 100 = 64 + 32 + 4 | =MOD(MOD(B10*B9,$B$3)*B6,$B$3) |
Excel's MOD function returns the same "math remainder" (0 or more) as this calculator. Even for negative numbers, =MOD(-7,3) is 2. If you need the C/Java-style remainder, use the last row of the first table, which uses TRUNC to round the quotient toward 0.
The first table divides −7 by 3: the remainder is 2 and the quotient is −3.
The second table checks 38 ≡ 14 (mod 12). The remainder of the difference 24 is 0, so it shows TRUE (congruent).
The third table finds the remainder of 7 to the 100th power divided by 13 by repeated squaring. The answer is 9. "^" is the power sign. Excel handles only about 15 digits exactly, so when the modulus n has more than 7 digits, the squared values lose precision. In that case, use the calculator on this page or Python.
How to calculate it in Google Sheets
| Dividend a | -7 |
| Modulus (divisor) n | 3 |
| Math remainder (0 or more) | =MOD(B1,B2) |
| Quotient (floor division) | =INT(B1/B2) |
| Check n × quotient + remainder | =B2*B4+B3 |
| C/Java-style remainder (rounded toward 0) | =B1-B2*TRUNC(B1/B2) |
| Integer a | 38 |
| Integer b | 14 |
| Modulus n | 12 |
| Difference a − b | =B1-B2 |
| Remainder of the difference ÷ n | =MOD(B4,B3) |
| Result (TRUE = congruent) | =MOD(B4,B3)=0 |
| Base a | 7 |
| Exponent k | 100 |
| Modulus n | 13 |
| Remainder of a^1 | =MOD(B1,B3) |
| Remainder of a^2 (square the row above, take the remainder) | =MOD(B4^2,$B$3) |
| Remainder of a^4 | =MOD(B5^2,$B$3) |
| Remainder of a^8 | =MOD(B6^2,$B$3) |
| Remainder of a^16 | =MOD(B7^2,$B$3) |
| Remainder of a^32 | =MOD(B8^2,$B$3) |
| Remainder of a^64 | =MOD(B9^2,$B$3) |
| Remainder combined for 100 = 64 + 32 + 4 | =MOD(MOD(B10*B9,$B$3)*B6,$B$3) |
How to calculate it in Python
a = -7
n = 3
# Python's % returns the same "math remainder" as this calculator (0 or more when the modulus is positive)
print(a % n) # 2
# divmod returns the floor-division quotient and the remainder together (a = n × quotient + remainder)
quotient, remainder = divmod(a, n)
print(quotient, remainder) # -3 2
# congruence check: is 38 ≡ 14 (mod 12)? (check whether the difference is a multiple of 12)
print((38 - 14) % 12 == 0) # True
# power mod: remainder of 7 to the 100th power divided by 13
# the 3-argument pow uses repeated squaring, so it finishes instantly even for huge exponents
print(pow(7, 100, 13)) # 9
def c_style_mod(x, m):
# same result as % in C, Java and JavaScript (quotient rounded toward 0)
r = x % m
if r != 0 and x < 0:
r -= m
return r
print(c_style_mod(-7, 3)) # -1
How to write it in LaTeX and other math languages (copy and paste)
a = n × q + r (0 ≤ r < n)
a = nq + r \quad (0 \le r < n)
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mrow>
<mi>a</mi><mo>=</mo><mi>n</mi><mi>q</mi><mo>+</mo><mi>r</mi>
<mo>,</mo>
<mn>0</mn><mo>≤</mo><mi>r</mi><mo><</mo><mi>n</mi>
</mrow>
</math>
a = n q + r, \ 0 <= r < n
{Quotient[a, n], Mod[a, n]}
q := floor(a/n); r := a mod n;
q = floor(a/n); r = mod(a, n);
a = nq + r (0 ≤ r < n)
a ≡ b (mod n) ⇔ a − b = n × m
a \equiv b \pmod{n} \iff a - b = nm
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mrow>
<mi>a</mi><mo>≡</mo><mi>b</mi>
<mspace width="0.3em"/>
<mo>(</mo><mi>mod</mi><mspace width="0.3em"/><mi>n</mi><mo>)</mo>
<mo>⇔</mo>
<mi>a</mi><mo>−</mo><mi>b</mi><mo>=</mo><mi>n</mi><mi>m</mi>
</mrow>
</math>
a -= b (mod n) iff a - b = n m
Mod[a - b, n] == 0
(a - b) mod n = 0;
mod(a - b, n) == 0
a ≡ b (mod n)
(a × b) mod n = {(a mod n) × (b mod n)} mod n
(a \times b) \bmod n = \{(a \bmod n)(b \bmod n)\} \bmod n
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mrow>
<mo>(</mo><mi>a</mi><mo>×</mo><mi>b</mi><mo>)</mo>
<mspace width="0.3em"/><mi>mod</mi><mspace width="0.3em"/><mi>n</mi>
<mo>=</mo>
<mo>{</mo>
<mo>(</mo><mi>a</mi><mspace width="0.3em"/><mi>mod</mi><mspace width="0.3em"/><mi>n</mi><mo>)</mo>
<mo>×</mo>
<mo>(</mo><mi>b</mi><mspace width="0.3em"/><mi>mod</mi><mspace width="0.3em"/><mi>n</mi><mo>)</mo>
<mo>}</mo>
<mspace width="0.3em"/><mi>mod</mi><mspace width="0.3em"/><mi>n</mi>
</mrow>
</math>
(a * b) mod n = ((a mod n) * (b mod n)) mod n
Mod[a b, n] == Mod[Mod[a, n] Mod[b, n], n]
(a * b) mod n = ((a mod n) * (b mod n)) mod n;
mod(a*b, n) == mod(mod(a, n)*mod(b, n), n)
(a×b) mod n = ((a mod n)×(b mod n)) mod n
a ≡ b, c ≡ d (mod n) ⇒ a+c ≡ b+d, a−c ≡ b−d, a×c ≡ b×d (mod n)
a \equiv b,\ c \equiv d \pmod{n} \Rightarrow a + c \equiv b + d,\ a - c \equiv b - d,\ ac \equiv bd \pmod{n}
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mrow>
<mi>a</mi><mo>+</mo><mi>c</mi><mo>≡</mo><mi>b</mi><mo>+</mo><mi>d</mi>
<mo>,</mo>
<mi>a</mi><mo>−</mo><mi>c</mi><mo>≡</mo><mi>b</mi><mo>−</mo><mi>d</mi>
<mo>,</mo>
<mi>a</mi><mi>c</mi><mo>≡</mo><mi>b</mi><mi>d</mi>
<mspace width="0.3em"/>
<mo>(</mo><mi>mod</mi><mspace width="0.3em"/><mi>n</mi><mo>)</mo>
</mrow>
</math>
a + c -= b + d, \ a - c -= b - d, \ a c -= b d (mod n)
Mod[a + c, n] == Mod[b + d, n] && Mod[a - c, n] == Mod[b - d, n] && Mod[a c, n] == Mod[b d, n]
(a + c) mod n = (b + d) mod n;
mod(a + c, n) == mod(b + d, n)
a + c ≡ b + d (mod n)
How to have ChatGPT do the calculation
You are a math calculation assistant for number theory (properties of integers). Do the following calculation by actually running Python code, and base your answer only on the numbers from the execution result (do not answer by mental math or guessing). Calculate the following 3 problems. 1. The math remainder of −7 divided by 3 (chosen to be 0 or more), and the quotient 2. Whether 38 ≡ 14 (mod 12) holds (also show whether the difference is a multiple of 12) 3. The remainder of 7 to the 100th power divided by 13 (use pow(7, 100, 13)) Use Python's %, divmod and pow(base, exponent, modulus), and show the formulas you used and the numbers from the execution result.
How to Use
-
1Enter your numbersType the numbers you want to calculate with into the input fields
-
2CalculatePress the "Calculate" button
-
3Check the resultThe result appears on the spot. The same page also explains the idea behind the calculation and the formula
DataChef Features
No technical knowledge required.
Intuitive and user-friendly operation.
Can be used without registering personal information.
Automatic file deletion by clicking "download".
and rapid file conversion.
No attribution required.
No need to contact us for commercial use permission.
