@arpit728 It can be easily solved, if you know that the number of multiples of any number(say x) occurs upto certain number(say n) is equal to the floor(n/x) (example: number of multiples of 4 upto 42 is floor(42/4)=10). In this question we can calculate number of multiples (between L and R) of each prime number less than N one by one. But this will include some numbers multiple time (example: for n=6, L=1 and R=32, 30 will be counted three times as multiple of 2, 3 and 5.), so we need to subtract that multiples of numbers formed by multiplying prime numbers two at a time. In above example, those numbers will be 2*3=6, 2*5=10 and 3*5=15, but this will remove 30 three times so we need to add the count of multiples formed by multiplying primes taken three at a time (i.e., 2*3*5=30).
So, it can be generalized as:
total count = \sum(count of multiples of primes taken one at a time) - \sum(count of multiples of product of prime taken two at a time) + … \sum(count of multiples of product of all prime numbers)
Example: N=6,L=1 and R=31; Let, count(x) = number of multiples of prime number x;
So, total = (count(2) + count(3) + count(5)) - ((count(2*3) + count(3*5) + count(2*5)) + ((count(2*3*5));
Final answer will be number of multiples till R - number of multiples till L, but in this example as L is 1 so we don’t need to calculate seperately for L.
count(2) = floor(31/2) = 15 (2,4,6,8,10,12,14,16,18,20,22,24,26,28,30)
count(3) = floor(31/3) = 10 (3,6,9,12,15,18,21,24,27,30)
count(5) = floor(31/5) = 6 (5,10,15,20,25,30)
count(2*3) = floor(31/6) = 5 (6,12,18,24,30)
count(3*5) = floor(31/15) = 2 (15,30)
count(2*5) = floor(31/10) = 3 (10,20,30)
count(2*3*5) = floor(31/30) = 1 (30)
total = (15+10+6) - (5+2+3) + (1) = 22