S1Q1 · Check if Either of Two Numbers is a Multiple of the Other¶
⚡ Quick Reference
Function: is_multiple(a: int, b: int) -> bool
Core idea: check if a % b == 0 or b % a == 0.
Key rules:
- a % b == 0 → a is a multiple of b
- b % a == 0 → b is a multiple of a
- Either condition makes the result True
Problem Statement¶
Problem
Write a function is_multiple(a, b) that returns True if either a is a multiple of b or b is a multiple of a.
Examples:
a=10, b=5
True
a=6, b=18
True
a=7, b=3
False
a=8, b=16
True
Tracing all examples¶
a |
b |
a % b |
b % a |
Either 0? | Result |
|---|---|---|---|---|---|
| 10 | 5 | 0 ✅ | 5%10=5 | ✅ | True |
| 6 | 18 | 6%18=6 | 18%6=0 ✅ | ✅ | True |
| 7 | 3 | 7%3=1 | 3%7=3 | ❌ | False |
| 8 | 16 | 8%16=8 | 16%8=0 ✅ | ✅ | True |
Solution approaches¶
Key takeaways¶
x % y == 0 means x is a multiple of y
If x divided by y leaves no remainder, then x is a multiple of y. Check both directions - a % b and b % a - to cover either case.
Short-circuit or - stops at first True
a % b == 0 or b % a == 0 - if the first condition is already True, Python skips evaluating the second. Efficient and clean.
Symmetric relationship
The result is the same regardless of argument order - is_multiple(10, 5) and is_multiple(5, 10) both return True. The or of both directions ensures this symmetry.