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 that both a and b are divisible by c.

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

Key rules: - Both conditions must hold → use and - Divisible by c% c == 0 - 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, and False otherwise.

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

Understanding the problem

Both conditions must be true simultaneously:

Condition Check
a is divisible by c a % c == 0
b is divisible by c b % c == 0

Use and - if either fails, return False.


Tracing all three 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)
    if a_divisible and b_divisible:
        return True
    else:
        return False
def is_both_divisible_by(a: int, b: int, c: int) -> bool:
    return all(x % c == 0 for x in [a, b])

all() checks that every element in an iterable satisfies the condition. Scales cleanly if you ever need to check more than two numbers.


Key takeaways

01

and for "both must hold"

Use and when every condition must be true. A single failure returns False. Short-circuits - if a % c != 0, the check for b is skipped entirely.

02

The trio: and / or / all()

and for two conditions both true. or for at least one true. all() for a variable number of conditions all true. Know when to use each.

03

Compare with Set 1 S1Q1 and Set 3 S1Q1

Set 3 S1Q1 used and (even AND second-last digit). Set 1 S1Q1 used or (even OR divisible by 5). This problem uses and again. Spotting the right operator is the key skill.