Skip to content

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.

def is_multiple(a: int, b: int) -> bool:
    return a % b == 0 or b % a == 0

Key rules: - a % b == 0a is a multiple of b - b % a == 0b 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:

Input
a=10, b=5
Output
True
Input
a=6, b=18
Output
True
Input
a=7, b=3
Output
False
Input
a=8, b=16
Output
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

def is_multiple(a: int, b: int) -> bool:
    return a % b == 0 or b % a == 0
def is_multiple(a: int, b: int) -> bool:
    a_is_multiple_of_b = a % b == 0
    b_is_multiple_of_a = b % a == 0
    return a_is_multiple_of_b or b_is_multiple_of_a
def is_multiple(a: int, b: int) -> bool:
    return any(x % y == 0 for x, y in [(a, b), (b, a)])
is_multiple = lambda a, b: a % b == 0 or b % a == 0

Key takeaways

01

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.

02

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.

03

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.