Calculate difference between multiples of two different numbers
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The difference between multiples of two numbers is usually written as a*m - b*n, where a and b are the base numbers and m and n are integer multipliers. That simple expression hides two different problems that are easy to mix up: computing one specific difference and asking what differences are possible in principle.
The Direct Formula
If you want the difference between the mth multiple of a and the nth multiple of b, compute:
Example:
This corresponds to:
- 4th multiple of 5 -> 20
- 2nd multiple of 7 -> 14
- difference -> 6
If you want an absolute difference instead of a signed one, use abs(...).
When the Same Multiplier Is Used
If both numbers use the same multiplier k, then:
That means the difference between the kth multiples grows linearly with k.
Example:
This is useful when you are comparing aligned sequences such as the first multiple of each, the second multiple of each, and so on.
The Smallest Positive Difference
If the question means "What is the smallest positive value that can be written as a multiple of a minus a multiple of b?", the answer is tied to the greatest common divisor:
For example, with a = 6 and b = 15:
- '
gcd(6, 15) = 3' - so the smallest positive difference between their multiples is
3
Python example:
This is a consequence of Bézout's identity: integer combinations of a and b generate exactly the multiples of gcd(a, b).
Brute-Force Example for Intuition
To see this numerically, list a few multiples:
Now compare:
- '
18 - 15 = 3' - '
30 - 30 = 0' - '
24 - 15 = 9'
The smallest positive difference you can achieve is indeed 3.
Use Cases
This kind of calculation shows up in:
- arithmetic sequence comparisons
- scheduling problems
- modular arithmetic
- Diophantine equations
For example, asking whether two periodic events can differ by exactly d units is closely related to whether d is a multiple of gcd(a, b).
That is the same reason these problems show up in clock arithmetic, gear ratios, and synchronization questions.
Writing a Helper Function
If you want a reusable function for the direct problem:
That handles both signed and absolute interpretations.
Relation to Least Common Multiple
Do not confuse difference with coincidence. If you want to know when two multiple sequences become equal, you are looking for the least common multiple:
The LCM answers "When do they meet?" The GCD answers "What step size do their differences live on?"
Common Pitfalls
The biggest mistake is not clarifying which problem you mean. "Difference between multiples" can mean a direct computed value such as a*m - b*n, or it can mean the smallest positive difference achievable across all choices of multipliers.
Another issue is forgetting whether sign matters. In some applications, -6 and 6 are meaningfully different; in others, only the absolute distance matters.
Finally, do not mix up GCD and LCM. GCD controls the structure of possible differences, while LCM controls when two sequences share the same multiple.
Summary
- The direct difference between specific multiples is
a*m - b*n. - If you use the same multiplier for both numbers, the expression simplifies to
(a - b) * k. - The smallest positive difference achievable by any multiples is
gcd(a, b). - The least common multiple answers equality of multiples, not difference size.
- Always clarify whether you want a signed difference, an absolute difference, or the minimal positive one.

