0
0
DSA Pythonprogramming~5 mins

Modular Arithmetic Basics in DSA Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is modular arithmetic?
Modular arithmetic is a way of doing math where numbers wrap around after reaching a certain value called the modulus. It's like a clock where after 12, it goes back to 1.
Click to reveal answer
beginner
What does the expression a % m mean?
It means the remainder when number a is divided by m. For example, 7 % 3 = 1 because 7 divided by 3 leaves remainder 1.
Click to reveal answer
beginner
How do addition and multiplication work in modular arithmetic?
You add or multiply numbers normally, then take the remainder after dividing by the modulus. For example, if modulus is 5, then (3 + 4) % 5 = 2 and (3 * 4) % 5 = 2.
Click to reveal answer
intermediate
Why is modular arithmetic useful in programming?
It helps keep numbers within a fixed range, avoids overflow, and is used in hashing, cryptography, and algorithms that need wrap-around behavior.
Click to reveal answer
intermediate
What is the result of (a - b) % m if a < b?
Even if a is less than b, (a - b) % m gives a non-negative remainder by wrapping around. For example, with m=5, (2 - 3) % 5 = 4.
Click to reveal answer
What is the value of 10 % 4?
A3
B0
C1
D2
If modulus is 7, what is (5 + 6) % 7?
A4
B11
C6
D0
Which of these is true for modular multiplication?
A(a * b) % m = a % b
B(a * b) % m = ((a % m) * (b % m)) % m
C(a * b) % m = (a % m) + (b % m)
D(a * b) % m = (a + b) % m
What happens if you do (a - b) % m and a < b?
AResult is negative
BResult is zero
CResult wraps around to a positive remainder
DError occurs
Why is modular arithmetic like a clock?
ABecause numbers reset after reaching modulus
BBecause it counts hours only
CBecause it uses 24-hour format
DBecause it only works with time
Explain modular arithmetic and how addition works with an example.
Think about how a clock resets after 12 hours.
You got /4 concepts.
    Describe why modular arithmetic is important in programming and give two examples of its use.
    Consider situations where numbers must wrap around or stay small.
    You got /4 concepts.