Tuesday, April 29, 2008

Solution to Problem 2

You can view the problem statement here.

Why did I ask this question now?
Because there are 3 good things in this problem :-
1. You need to understand the problem.
2. There is a lot you can do to optimize the solution
3. If you don't optimize the solution, you may end up screwing your reputation in your job interview.

Remember, anyone can code. But the way anyone will differentiate between you and an average coder is on the basis of

  • How well you handle the boundary conditions. (Does your code fail for any input)

  • How efficient your code is.

  • And how well can you optimize your code.



Many people coming to this blog are students who would be writing programs in their job interviews soon. I can tell from personal experience that the sexiest of companies only ask for programmers who code keeping the above points in their mind.

Ok now coming back to the problem.

We can check for primality by iterating from 2 to (n-1) and if it is divisible by any number in between, we can be sure that it is not a prime number.



However, there is a lot of unnecessary iteration over here.
You might be thinking that, since we are starting from 2, we can stop iterating when i reaches (n/2).



Well you are right. But the number of iterations can be optimized further.

Consider, a number 21. Lets list down its divisors
1 x 21 = 21
3 x 7 = 21
7 x 3 = 21
21 x 1 = 21
After removing the first and the last case. We are left with only 3 x 7 and 7 x 3. Notice the repetition. Once we have 3 we dont need to go till 7 do we?
Now you would argue that, our program will automatically come out of the loop at 3 so there is no question of reaching all the way till 7. Well you are right again. But the problem doesn't come when the input is composite (A number that is not prime). The problem comes when the number is prime. Take 23 for example.
Try dividing it with :-
2 - No
3 - No
4 - No
5 - No
.
.
.
11 - No
And then we come out of the loop.

But do we really need to go all the way till 11?
No we don't.
Applying some common sense, you can sense that after the square root of the number, the order of multiplication starts changing.
Let me illustrate that with an example.

Consider n = 24
2 x 12 = 24
3 x 8 = 24
4 x 6 = 24
6 x 4 = 24
8 x 3 = 24
12 x 2 = 24

The divisors start repeating after i becomes greater than 5. Its basically because, i becomes > 4.89 or Square Root(24). So we only need to iterate till the square root of a number.

Here is the code for the problem.



Now coming back to the 3 points that I had mentioned earlier on why this problem is good.
1. You need to understand the problem. Did you know that, prime numbers can't be negative? I didn't know it when I was asked to write this program the first time.
2. There is a lot you can do to optimize the solution You already have an idea of the optimization now.
3. If you don't optimize the solution, you may end up screwing your reputation in your job interview. If you apply for a job in a company like Google, Microsoft, Trilogy or an uber-cool startup. They are really gonna see how well you are able to optimize your solutions.

Can you find out a way of making the function return still faster for a random input?
Thats your assignment.

Thats all for the day.
Trust you found the post interesting.

Full_Metal

2 comments:

Unknown said...

hey bhaiya this is really cool..... the way u wrote the solution to this problem was really nice.... i mean the wikipedia link for the meaning of prime n those points on optimizing the solution...... it was really nice.... ummm one last thing.... dnt u think it wud be better if u write the problem statement first followed by its solution......??

full_metal said...

Thank you Satbir!
Glad that you liked it :P

I do give a link to the question right before the solution. This is for 2 reasons. First, if the problem takes the person to a page where the solution cannot be seen anymore, then he/she would be encouraged to solve the problem on his/her own. Secondly, Its a good practice to not repeat something thats been mentioned before while blogging.