Question - https://hack.codingblocks.com/contests/c/537/63
My solution - https://ide.codingblocks.com/s/37748
Passed for the first two cases but TLE in the third case . How can I optimize it ?
Question - https://hack.codingblocks.com/contests/c/537/63
My solution - https://ide.codingblocks.com/s/37748
Passed for the first two cases but TLE in the third case . How can I optimize it ?
There is mistake in your logic of isPrime as well…
For the input
2
1 10
11 20
Your output is
4
5
But the correct output should be
4
4
In loop you started from 3, but it has to be started from 2 only. That’s why it gives wrong answer
You can optimize the code further by making an array and saving prime numbers in it beforehand.
Then you just have to traverse array and count the number of primes in the range as per the question.
You have to optimise your code. Try doing it in logn.
Try to precompute the prime numbers, make a visited array which stores the information whether a number is prime or not beforehand.
Hint: you can do this with the help of Sieve Of Eratosthenes algorithm, which has time complexity O(n*log(log(n))).