Skip to content

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.

def is_both_divisible_by(a: int, b: int, c: int) -> bool:
    return a % c == 0 and b % c == 0

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:

Input
a=10, b=15, c=5
Output
True
Input
a=10, b=13, c=5
Output
False
Input
a=19, b=15, c=5
Output
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

def is_both_divisible_by(a: int, b: int, c: int) -> bool:
    return a % c == 0 and b % c == 0
def is_both_divisible_by(a: int, b: int, c: int) -> bool:
    a_divisible = a % c == 0
    b_divisible = b % c == 0
    return a_divisible and b_divisible
def is_both_divisible_by(a: int, b: int, c: int) -> bool:
    return all(n % c == 0 for n in (a, b))

Scales naturally if the problem were extended to more numbers.

is_both_divisible_by = lambda a, b, c: a % c == 0 and b % c == 0

Key takeaways

01

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.

02

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.

03

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.