I was asked to print all the prime numbers between given two numbers. The first code that comes to our mind is to run a loop and find all the prime numbers by dividing each number with every other number. Mind you, this takes a hell lot of time. One can optimize the code easily by discluding the numbers that get divided by 2,3,5. This takes away numbers out from the loop that prevent us from checking them. The time limit set hence becomes a little reachable.
The second thing is logic. One can even look at the problem at different angle. You can yourself Google and find that there are many other good methods of finding a prime number. One method needs the loop to run only for (n)^(1/2) times. This also reduces the time a great deal.
CODE IT.. OPTIMIZE IT!!
* * * * *
* * * * *
No comments:
Post a Comment