Codility
programming
algorithms
multiples
coding challenge

Codility test - find multiples in range

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The Codility "count the multiples in a range" problem looks simple, but the whole point is to avoid scanning every number. The efficient solution is arithmetic, not iteration. Once you count how many multiples exist up to the upper bound and subtract how many exist below the lower bound, the answer drops out in constant time.

Derive the Constant-Time Formula

Suppose the range is inclusive from A to B, and you want the numbers divisible by K.

The count of multiples of K from zero up to B is:

B // K

The count of multiples of K from zero up to A - 1 is:

(A - 1) // K

So the count inside the inclusive interval from A to B is:

B // K - (A - 1) // K

This works because integer division tells you how many full groups of size K fit into the bound.

Implement the Solution Directly

Here is a compact Python implementation:

python
1def count_divisible(A: int, B: int, K: int) -> int:
2    if K <= 0:
3        raise ValueError("K must be positive")
4
5    if A == 0:
6        return B // K + 1
7
8    return B // K - (A - 1) // K
9
10
11print(count_divisible(6, 11, 2))
12print(count_divisible(0, 11, 2))

The first example returns 3 because the multiples are 6, 8, and 10. The second returns 6 because 0 is also divisible by every positive integer.

If you prefer a version without the special case, you can write the formula a little differently:

python
def count_divisible(A: int, B: int, K: int) -> int:
    return B // K - ((A - 1) // K if A > 0 else -1)

The separate A == 0 branch is often clearer in interviews and coding tests.

Why a Loop Is the Wrong Tool

The brute-force version would check every value from A to B and test value % K == 0. That works for small input sizes, but it is unnecessary and too slow when the interval is large.

For example, iterating from 0 to one billion just to count divisibility would be far slower than a pair of integer divisions. Codility problems are usually designed to reward the arithmetic insight rather than a straightforward loop.

This is one of those tasks where the correct algorithmic idea matters much more than low-level code tricks.

Edge Cases That Matter

The two most important edge cases are:

  • 'A equals 0'
  • 'K is invalid'

0 must be handled carefully because it is divisible by any positive K, and the expression (A - 1) // K becomes easy to misreason about if you are not deliberate.

Also, K = 0 is not mathematically meaningful for divisibility, so good code should reject it rather than returning nonsense.

The formula assumes A <= B. If the caller can violate that, validate it explicitly or decide on a policy such as returning zero.

Common Pitfalls

The most common mistake is writing a loop and passing the easy sample tests while missing the intended time complexity.

Another issue is forgetting that the range is inclusive. The whole point of subtracting counts up to A - 1 is to preserve inclusion at both ends.

People also frequently mishandle A = 0. Since zero is divisible by K, that case needs to be thought through rather than assumed away.

Summary

  • The efficient solution counts multiples with arithmetic instead of iterating through the range.
  • The main formula is B // K - (A - 1) // K.
  • Handle A = 0 carefully because zero itself is divisible by any positive K.
  • Reject K = 0 because divisibility by zero is undefined.
  • This is a constant-time solution, which is what Codility-style tests are usually looking for.

Course illustration
Course illustration

All Rights Reserved.