S1Q1 · Check If a Number Divides Two Other Numbers¶
⚡ Quick Reference
Function: is_both_divisible_by(a: int, b: int, c: int) -> bool
Core idea: check if c divides both a and b using the modulo operator.
Key rules:
- x % c == 0 means c divides x exactly
- Both conditions must hold - use and
- c is guaranteed non-zero - no guard needed
Problem Statement¶
Problem
Write a function is_both_divisible_by(a, b, c) that returns True if both a and b are divisible by c.
Examples:
a=10, b=15, c=5
True
a=10, b=13, c=5
False
a=19, b=15, c=5
False
Tracing all examples¶
a |
b |
c |
a % c |
b % c |
Result |
|---|---|---|---|---|---|
| 10 | 15 | 5 | 0 ✅ | 0 ✅ | True |
| 10 | 13 | 5 | 0 ✅ | 3 ❌ | False |
| 19 | 15 | 5 | 4 ❌ | 0 ✅ | False |
Solution approaches¶
Scales naturally if the problem were extended to more numbers.
Key takeaways¶
x % c == 0 is the divisibility test
The modulo operator gives the remainder. A remainder of 0 means c divides x exactly. This is the standard Python divisibility check.
Short-circuit and - skip second check if first fails
a % c == 0 and b % c == 0 - if a is not divisible by c, Python skips checking b entirely. The result is already False.
all() for extensibility
all(n % c == 0 for n in (a, b)) generalises naturally - add more numbers to the tuple without changing the logic. Good habit when checking the same condition across multiple values.