easy +10 pts

Count Multiples in a Range

Count how many integers in a range are divisible by a given divisor.

Write a function `count_multiples(start, end, divisor)` that takes three integers: `start`, `end`, and `divisor`. The function should return the number of integers `n` such that `n` is between `start` and `end` inclusive and `n` is divisible by `divisor`. Assume `divisor` is non-zero. The range includes both endpoints. For example, between 1 and 10 inclusive, the multiples of 3 are 3, 6, and 9, so the count is 3.

Constraints

- `start` and `end` can be any integers (may be negative, and `start` may be greater than `end`; if `start > end`, treat the range as empty and return 0). - `divisor` is a non-zero integer. - The range size can be up to 10^6; your solution should complete in reasonable time (O(n) looping is acceptable but an O(1) formula is preferred). - The function must return an integer.

Example

>>> count_multiples(1, 10, 3)
3
>>> count_multiples(1, 10, 5)
2
>>> count_multiples(1, 10, 1)
10
>>> count_multiples(1, 10, 7)
1
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Consider the case where start > end: return 0 immediately.
You can loop from start to end and check divisibility using modulo.
To avoid looping, count how many multiples exist by dividing the endpoints by divisor, but careful with negative numbers.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.