Modular Arithmetic Calculator

Counting that wraps round at a fixed number, the way a clock runs past twelve and back to one. It gives the true answer even for negatives, where -7 on a three-hour clock is 2 rather than the -1 most programming languages hand back.

How to use it

You pick the job first: reducing a number, raising one to a power, finding an inverse, or testing whether a number is prime.

  1. Pick the operation Reduction, exponentiation, inverse or a primality test.
  2. Enter the numbers Whole numbers only. Negatives are handled correctly, which is the point.
  3. Read the check The inverse shows the product that proves it, so nothing is taken on trust.
Full instructions
In depth

A practical guide to modular arithmetic

Modular arithmetic is the maths behind clocks, cryptography and a surprising amount of everyday programming. This article explains each operation the calculator offers, shows how they connect to real problems, and covers the mistakes that trip people up most often.

Screenshot: Clock arithmetic, and the cryptography it enables
Clock arithmetic, and the cryptography it enables as it appears when the page opens.

Thinking in circles instead of lines

Normal arithmetic runs on a number line that goes on forever. Modular arithmetic bends that line into a circle. Pick a modulus of 12 and every number lands on one of the spots from 0 to 11. After 11 the count goes back to 0, just like a clock goes back to midnight after eleven at night. That is why people often call it clock arithmetic. The numbers go round and round, and the modulus decides how big the circle is.

This idea is far older than computers. Gauss wrote about it in 1801, and clocks have used it for even longer. What makes it matter today is that almost every secure link on the internet depends on operations that are easy to do going forward on this circle but almost impossible to undo. The calculator on this page handles four of those operations. All of them run with exact whole-number maths, so the results are always right down to the last digit.

Reduction: the simplest operation

Reducing a number modulo m means finding where it lands on the circle. Twenty-seven reduced modulo 12 is 3, because 27 is two full laps around the clock plus 3. The calculator calls this operation a mod m. You type a value into the a field and a modulus into the m field, and the answer appears right away. It handles both positive and negative inputs.

Negative numbers are where most programming languages get the answer wrong. In almost every language, the percent operator gives a remainder, not a true modulus. Ask a typical language for negative 7 mod 3 and it says negative 1. But on a clock, three steps before the 2 is the 11, not the negative 1. This calculator always gives the true modulus: the non-negative result you need for correct clock arithmetic. This single mistake causes real bugs in code that wraps around lists, rotates angles or picks slots in a ring. If you have ever seen an off-by-one that only showed up with a negative index, this is probably why.

Fast exponentiation without giant numbers

Raising a number to a large power normally makes something huge. Seven to the power of a thousand would have more than 800 digits if you wrote it out. But if you only need the answer modulo 13, there is no need to build that giant number at all. The trick is to square and reduce at each step, so the working value stays small the whole time.

The calculator does this with an approach called repeated squaring. At each step it squares the result so far and right away takes the modulus, so nothing ever gets bigger than the modulus squared. Even a power of a thousand only needs about ten of these steps. This is the idea at the core of RSA. Raising a message to a public exponent modulo a large number is fast and easy. Going backward to find the original message, without the private key, would take longer than a lifetime. That gap between doing and undoing is what keeps the system secure.

Modular inverses and why they matter

You cannot divide in modular arithmetic the way you divide normal numbers. Instead, you find a modular inverse. The inverse of a number a, modulo m, is the value that you multiply a by to get 1 on the circle. For example, the inverse of 3 modulo 7 is 5. Why? Because 3 times 5 is 15, and 15 modulo 7 is 1. It works.

Not every number has an inverse. It only exists when the number and the modulus share no common factor other than 1. When two numbers meet that test, they are called coprime. If you ask for the inverse of 4 modulo 8, the calculator will tell you there is none, because 4 and 8 both divide by 2. The result also shows the product that proves the inverse is right, so you can check it at a glance instead of just trusting the output.

Modular inverses are what make decryption possible in RSA. The private key is the inverse of the public exponent, worked out modulo a value that comes from two secret prime numbers. Without those primes, finding the inverse is not feasible.

Testing whether a number is prime

The fourth operation checks whether a number is prime. For small numbers you could test by dividing it by every smaller number up to its square root. That works, but it gets very slow when the number is large.

The calculator uses a faster method called Miller-Rabin. It runs a set of modular power checks against a few chosen test values. With the right fixed set of those values, the test gives a clear yes or no for every number below a very large limit. It is not guessing. For numbers in its range the answer is certain, and the whole check finishes in a blink even for numbers with many digits.

Knowing whether a number is prime matters in many places. Key generation for secure systems needs large primes. Hash tables work best when their size is a prime number. And some algorithms in coding theory depend on prime fields. The calculator handles this check right on the same page, so you can test a number and then jump to the other operations to work with it further.

How to use each control on the page

The panel on the right side has a dropdown labelled Operation with four choices. The first, a mod m, takes two inputs: a and the modulus m. The second, a to the power b mod m, adds a third input for the exponent. The third finds the inverse of a modulo m, and the fourth tests whether a is prime, ignoring the other fields.

All three number fields, labelled a, b and m, accept whole numbers including negative values. The b field is only used for the power operation and the hint text underneath it says so. Results appear right away as you change any input, so you can try different numbers quickly without pressing a button each time. The Reset button in the panel header clears everything back to the starting values of 7, 1000 and 13, which is a good combination for seeing the power operation in action. The entire calculation happens in your browser using exact integer arithmetic, so nothing is sent to a server and nothing rounds or loses digits.

Everyday code where this comes up

Programmers run into modular arithmetic more often than they might think. Wrapping an index around a list is a modulus. Working out which day of the week falls on a given date is a modulus. Spreading items evenly across columns is a modulus. Building a hash code is a modulus. In every one of these jobs, the remainder operator that most coding languages give you produces the wrong answer for negative inputs. This calculator shows what the right answer looks like, so you can spot the bug before it ships.

Cryptographic code is the other big area. Key generation, digital signatures and key-exchange protocols all lean on modular powers and modular inverses. When you are building or testing that kind of code, having a quick way to check a result by hand is very helpful. Plug the same inputs into the calculator, compare the outputs, and you know whether your code is on track. It takes seconds and can save hours of debugging.

Precision and the role of big integers

Regular floating-point maths rounds large numbers. That might seem harmless, but a single rounded digit makes a modular result completely wrong. If you work out 7 to the power of 100 in regular floating point and then take the modulus, the answer will be garbage because digits were lost along the way. Modular maths is all or nothing. Every digit matters.

The calculator avoids that trap by using big integers for everything. Every step, every product and every comparison uses exact whole-number maths that keeps every digit. This is why you can type in numbers with dozens of digits and still trust what comes back. The whole thing runs on your own device, in your browser. Nothing is sent to a server and nothing is stored anywhere.

Help

Clock arithmetic, and the cryptography it enables

You pick the job first: reducing a number, raising one to a power, finding an inverse, or testing whether a number is prime. Powers are built up step by step rather than by forming the enormous intermediate and then cutting it down, so very large exponents stay workable, and primality is settled with Miller-Rabin.

Pick the operation

Reduction, exponentiation, inverse or a primality test.

Enter the numbers

Whole numbers only. Negatives are handled correctly, which is the point.

Read the check

The inverse shows the product that proves it, so nothing is taken on trust.

Exact throughout

Everything uses BigInt, so nothing rounds — which matters, because modular results are meaningless if a single digit is wrong.

Good to know

Remainder is not modulus

Almost every language gives -7 % 3 = -1. Three hours before two o'clock is eleven, not minus one, and this is the commonest bug in code touching modular maths.

Exponentiation is cheap, undoing it is not

Squaring and reducing at each step handles 7^1000 mod 13 in about ten multiplications. That asymmetry is what RSA is built on.

Inverses need coprimality

A number has an inverse mod m only when they share no common factor. It is the modular version of dividing.

Common questions

What is a modular inverse for?
It is how you divide in modular arithmetic, and it is literally what a decryption key is in RSA — the inverse of the encryption exponent.
Why is Miller–Rabin used rather than trial division?
Trial division on a large number takes forever. With the right fixed bases Miller–Rabin is deterministic and correct below 3.3 × 10²⁴, and near-instant.
What does "coprime" mean?
Sharing no common factor except 1. 8 and 15 are coprime despite neither being prime.
Why does 7^1000 mod 13 not overflow?
Because the number is never formed. Each squaring is reduced mod 13 immediately, so nothing ever exceeds 169.