Integers divisible by primes

Write codeMedium25 min

Your task

Given an integer n and a list of distinct primes, count how many integers in [1, n] are divisible by at least one of the given primes. Summing floor(n/p) over-counts numbers divisible by several primes, so apply the inclusion-exclusion principle over all non-empty subsets of the primes: add floor(n / product) for odd-sized subsets and subtract for even-sized ones. The list is small enough to enumerate subsets.

Examples

count_divisible_by_any(n: int, primes: list[int]) -> int
Example 1
Inputcount_divisible_by_any(10, [2, 3])
Output7