# Given a number $N$ and a prime $P$, how many numbers $\leq N$ are divisable by P but not by any smaller primes?

The following Math Exchange question deals with a similar problem:

Not divisible by $2,3$ or $5$ but divisible by $7$

However, the answers given become infeasible quite quickly because the amount of permutations increases too rapidly as P grows.

• It doesn't become infeasible sooo quickly, because the products of primes in the inclusion-exclusion grow pretty fast. What sort of $N$ and $P$ are you interested in? – Daniel Fischer Jun 29 '15 at 8:51
• I'm working with N in the range of 10^9 and P in the range of 10^4, for example. – Nicolás Siplis Jun 29 '15 at 8:54
• Then you have around $1000$ primes less than $P$ to consider, and the products of such primes you need to consider do not exceed about $10^5$. That's products of at most six primes. With a good dynamic programming approach to break out as soon as you can, that is feasible. At least unless you have very tight time constraints. If you don't need an exact count and an approximation suffices, Mertens' third theorem becomes relevant. – Daniel Fischer Jun 29 '15 at 9:05
• I considered applying Merten's third theorem but I figured inclusion-exclusion would yield a better answer. – Nicolás Siplis Jun 29 '15 at 9:07
• Wait. The quotient $N/P$ is only about $10^5$, when $P$ is near the upper end. That calls for sieving. Take the DP approach when $P$ is smaller. – Daniel Fischer Jun 29 '15 at 9:08