LearnViz CompSci
Home / Learn / Theory & Discrete Math / Modular Arithmetic

Beginner Topic 15 of 51 Theory & Discrete Math

Modular Arithmetic

Arithmetic where numbers wrap around a modulus.

How it works

Modular arithmetic works with remainders after division by a modulus n, where numbers wrap around like a clock. Two integers are congruent modulo n if they differ by a multiple of n. Addition, subtraction and multiplication respect congruence, so results can be reduced at any point. Division requires a modular inverse, which exists exactly when the number is coprime to the modulus, found with the extended Euclidean algorithm. Results include Fermat's little theorem and the Chinese remainder theorem.

Step-by-step walkthrough

  1. Modular arithmetic wraps numbers around a modulus like a clock.
  2. 12 divided by 5 leaves remainder 2.
  3. 17 also leaves remainder 2.
  4. So 12 and 17 are congruent modulo 5.
  5. Addition and multiplication work on remainders, the base of hashing and cryptography.

Where it is used

Cryptography such as RSA and Diffie-Hellman, hash functions, checksums and ISBN digits, random number generators, calendar calculations and circular buffers use modular arithmetic.

Common pitfall

Languages differ in the sign of the remainder for negative numbers, so -7 % 3 may be -1 or 2. Dividing without an inverse is invalid. Overflow can occur when multiplying large values before reducing, so reduce at each step or use wider types. Choose prime moduli when you need division.

Where it fits in the course

Modular Arithmetic is topic 15 of 51 in Theory & Discrete Math, rated Beginner. It follows Graph Coloring. The next topic is Number Theory Basics.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Theory & Discrete Math quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Modular Arithmetic?
Arithmetic where numbers wrap around a modulus. It belongs to Theory & Discrete Math and is a beginner-level topic.
Where do people use Modular Arithmetic?
Cryptography such as RSA and Diffie-Hellman, hash functions, checksums and ISBN digits, random number generators, calendar calculations and circular buffers use modular arithmetic.
What is the most common mistake with Modular Arithmetic?
Languages differ in the sign of the remainder for negative numbers, so -7 % 3 may be -1 or 2.
What should I learn before this topic?
Start with Graph Coloring, the topic just before it in Theory & Discrete Math.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Theory & Discrete Math quiz.
Is Modular Arithmetic hard to learn?
It is a Beginner topic, so it works as an entry point. If the terms are new, read the explanation twice and try the visualizer before the quiz.
How long does it take to learn Modular Arithmetic?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Modular Arithmetic?
Graph Coloring, Number Theory Basics, Euler and Hamilton Paths are closely connected. The full list is in the related topics section of this page.
What should I study after Modular Arithmetic?
Continue with Number Theory Basics, the next topic in Theory & Discrete Math.

Related topics