Check whether one of two positive integers divides the other evenly
Given two strictly positive integers a and b, return 1 if either one divides the other with no remainder, otherwise return 0. The division may go in either direction — a into b or b into a counts.
Examples: a=1, b=10 → 1; a=15, b=6 → 0.
def divides(a: int, b: int) -> int:
# your code here
Write the implementation.
Test both directions of the modulo: return 1 if (b % a == 0 or a % b == 0) else 0. Either remainder being zero means one number divides the other. For a=1, b=10, 10 % 1 == 0 → 1; for a=15, b=6, neither 15 % 6 nor 6 % 15 is zero → 0.
- ✗Checking only one division direction
- ✗Confusing divisibility with sum parity
- ✗Assuming only equal numbers divide evenly
- →Why is no zero-division guard needed for positive inputs?
- →How would the answer change if zero were allowed?
Either number may divide the other, so test both remainders.
def divides(a: int, b: int) -> int:
return 1 if (b % a == 0 or a % b == 0) else 0
For a=1, b=10: 10 % 1 == 0, so the result is 1. For a=15, b=6: 15 % 6 == 3 and 6 % 15 == 6, neither is zero, so the result is 0. Because both inputs are guaranteed strictly positive, % a and % b are always safe.