sieve is O(N) while
checking for prime for each no between n and m is O( (N-M) * sqrt(N) ).
why does the latter pass all tests and not the former.
Deeapk and Primes 2
can anyone explain the above doubt?
if you see the constraints… n<=10^9 while N-M<=10^6
so O(N) is very much larger than O((N-M)sqrt(N))
thanks
But
O(N) = 10^9 and
O((N-M)* sqrt(N))= 10^5*sqrt(10^9)>10^9
the time complexity you mentioned is incorrect.
its not (N-M)sqrt(N) , its infact (N-M)loglogN.
think about it.
say x=N-M
time complexity is:x/2+x/3+x/5+x/7+…x/p… p is prime no
thanks
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.