NOT SO EASY MATH

Can you please explain the question statement? I am bit confuse how for n=10 output is 9.

Hello, @rahul_bansal,

According to the problem statement, you have a number n and you need to find the count of numbers from 1 to n that are divisible by prime numbers less than 20,

An array of Prime numbers less than 20 are [2, 3, 5, 7, 11, 13, 17, 19]
Now, any number from 1 to n if divisible from any element in the array will increase the count.

Now, let’s take N = 10,

 1 (not divisible by anyone)
 2 (divisible by 2)
 3 (divisible by 3)
 4 (divisible by 2)
 5 (divisible by 5)
 6 (divisible by 2 and 3)
 7 (divisible by 7)
 8 (divisible by 2)
 9 (divisible by 3)
 10 (divisible by 2 and 5)

So, except 1 every element is divisible by some elements from the array.
So, ans is 9

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

CODE: https://ide.codingblocks.com/s/649895

This code is from lecture and giving the wrong output.

PS: Can you please explain this code also.

@rahul_bansal,
okay let me see

@rahul_bansal,

There is one error, below statements need to be written after the second for loop is executed.

if(setBits&1)
ans += n/denom;
else
ans -= n/denom;

Here is the updated code,
Updated Code for Inclusion Exclusion