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
- Modular arithmetic wraps numbers around a modulus like a clock.
- 12 divided by 5 leaves remainder 2.
- 17 also leaves remainder 2.
- So 12 and 17 are congruent modulo 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
- Open the interactive lesson, with a mini quiz.
- Take the Theory & Discrete Math quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- 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?
Where do people use Modular Arithmetic?
What is the most common mistake with Modular Arithmetic?
What should I learn before this topic?
How can I practice it?
Is Modular Arithmetic hard to learn?
How long does it take to learn Modular Arithmetic?
Which topics are related to Modular Arithmetic?
What should I study after Modular Arithmetic?
Related topics
- Graph Coloring (Theory & Discrete Math)
- Number Theory Basics (Theory & Discrete Math)
- Euler and Hamilton Paths (Theory & Discrete Math)
- Alphabets and Languages (Theory & Discrete Math)
- Arithmetic Logic Unit (Computer Architecture & Logic)