Please check my code
Sure. On it. Thanks for your patience.
Hey @Bhuwan-Agrawal-2362769150606526
There seems to be logical errors with your code. The logic you are using to solve the question is not correct. Check this link once to get hint Code optimization
hello Sir are u there??
sir please check my code once again because I am using your approach only
And for optimisation I give you a hint. The number of numbers between 1 and N divisible by a number P is equal to N / P.
Say you have the array of only first 2 prime numbers. {2, 3}
Number of numbers divisible by 2 be int d2 = N / 2;
Number of numbers divisible by 3 be int d3 = N / 3;
But the answer is not equal to d2 + d3 as now there is some overlapping.
Because now numbers between 1 and N that are divisible by 6 are counted twice.
So we need to subtract those numbers. So,
Number of numbers divisible by 6 be int d6 = N / 6;
So answer for this problem will be d2 + d3 - d6 (int ans = d2 + d3 - d6;).
This was just for 2 and 3, you need to do this for the whole array of {2, 3, 5, 7, 11, 13, 17, 19}.
I checked your code again. You’re using another strategy. If the strategy you’re using was mentioned in the CB video please send me the link to that video and I can check against that technique then.
i am using this approach
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.
@Bhuwan-Agrawal-2362769150606526 what you are doing is , for any number n, you are creating a series n/2-n/3+n/23 …, this is wrong. Either you are applying the bitmask principle stated in the video wrongly or you are using wrong principal,(as I do not have access to video so I cannot tell you more, please raise the doubt for the video if you want to understand the principle stated in video).
As for solving this problem you need to follow the approach told above, you can simple find all the pairs (23, 24…, 235, 237 … so on using simple for loop (as the number of primes is less this would work perfectly). After finding all the pairs, simple do n/2 +n/3+n/5…-n/23 -n/25 - n/27 - … + n/235… so on(basically add the number using even number of primes, subtract the number using odd number of primes)
ok sir I will raise the doubt in the video only