yesterday i code something in Python , after spending some time with it i realize that it made job of programmer quite easy, though Python , perl are scripting languages but very powerful. To handle very long numbers in c/c++ is not an easy task even simple arithmetic of huge number which we do using string is not very easy , it need a close observation and long code but in python it took only 10-12 lines.
While working on a problem i normally check the time taken by best solution , strangely people achieved unbelievable small time, i always amazed by seeing this, what data structure they use , what algorithm they use. After seeing that i always tell me that i have a loooooooooong way to go :)
Thursday, September 25, 2008
Saturday, September 6, 2008
Factorial !!!!!!!!!!!!
Today i saw a problem which was asked often in technical round of good companies.
Problem : Finding the first non zero digit in the factorial of a number ( for 5! = 120 first non zero digit is 2 ).
Solution : If number is such that its factorial is within the range of even long long then we can use any brute force method like removing digit one by one till we don't get first non zero digit ( though it wont be efficient ).
Here is better solution , i am not claiming it is best , if any one know better please comment it.
We can easily calculate number of zero in the factorial of a number by dividing the number first by 5 then 5 ^ 2 then 5 ^ 3 .......and adding result till 5^i < number.
Let we get that number has n number of zero then we are sure that the number has at least n number of 2 and n number of 5. What we have to do is to just remove n number of 2 and 5 from given number and keep track of last digit , till we get the answer.
here is the working code
int main ()
{
int Num2, Num5;
int Ldigit = 1, tmp;
int Num, NumZero = 0, i;
scanf ( " %d", &Num );
for ( i = 1; ;i++ )
{
tmp = pow ( 5, i );
if ( tmp > Num )
break;
NumZero += Num / tmp;
}
Num2 = Num5 = NumZero;
for ( i = 2; i <= Num; i++ )
{
tmp = i;
while ( !(tmp % 2) && (Num2>0) )
{
tmp /= 2;
Num2--;
}
while ( !(tmp % 5) && (Num5>0) )
{
tmp /= 5;
Num5--;
}
Ldigit *= tmp;
Ldigit %= 10;
}
printf ( " Last Digt = %d\n", Ldigit );
return 0;
}
Problem : Finding the first non zero digit in the factorial of a number ( for 5! = 120 first non zero digit is 2 ).
Solution : If number is such that its factorial is within the range of even long long then we can use any brute force method like removing digit one by one till we don't get first non zero digit ( though it wont be efficient ).
Here is better solution , i am not claiming it is best , if any one know better please comment it.
We can easily calculate number of zero in the factorial of a number by dividing the number first by 5 then 5 ^ 2 then 5 ^ 3 .......and adding result till 5^i < number.
Let we get that number has n number of zero then we are sure that the number has at least n number of 2 and n number of 5. What we have to do is to just remove n number of 2 and 5 from given number and keep track of last digit , till we get the answer.
here is the working code
int main ()
{
int Num2, Num5;
int Ldigit = 1, tmp;
int Num, NumZero = 0, i;
scanf ( " %d", &Num );
for ( i = 1; ;i++ )
{
tmp = pow ( 5, i );
if ( tmp > Num )
break;
NumZero += Num / tmp;
}
Num2 = Num5 = NumZero;
for ( i = 2; i <= Num; i++ )
{
tmp = i;
while ( !(tmp % 2) && (Num2>0) )
{
tmp /= 2;
Num2--;
}
while ( !(tmp % 5) && (Num5>0) )
{
tmp /= 5;
Num5--;
}
Ldigit *= tmp;
Ldigit %= 10;
}
printf ( " Last Digt = %d\n", Ldigit );
return 0;
}
Tuesday, September 2, 2008
Integer Overflow
Yesterday i was working on a problem where we have to check a number is whether prime or not , for that i gone through two famous prime testing algorithm , but i was getting wrong answer , i was trying hard but nothing worked , then i searched on internet , where i found very interesting thing.
It is well known that when we have to find the value of a raise to power n , we can calculate it in O ( log n ) time , we can apply same logic when we have to get a * b, what ever we did to n same thing we do to b here. This was the culprit as when i was calculating a * b it was going beyond integer range ( Actually we apply this logic when we have to found modulo of a * b otherwise it is of no use because it will grow beyond longest integer value ).
But above logic won't work if 2 * a goes beyond longest integer range, java provide us with facility to create our own custom integer .
It is well known that when we have to find the value of a raise to power n , we can calculate it in O ( log n ) time , we can apply same logic when we have to get a * b, what ever we did to n same thing we do to b here. This was the culprit as when i was calculating a * b it was going beyond integer range ( Actually we apply this logic when we have to found modulo of a * b otherwise it is of no use because it will grow beyond longest integer value ).
But above logic won't work if 2 * a goes beyond longest integer range, java provide us with facility to create our own custom integer .
Saturday, August 30, 2008
Gossiper and Me :(
Normally i like to talk a lot , here came the problem. I found a problem Gossiper , u can refer here https://www.spoj.pl/problems/GOSSIPER/ , i had to spent a lot of time on it. I coded it twice but nothing was working, i was thinking continuously about it in such an instant that i was not able to concentrate on other things , i had some intuition about it but i was not getting a clear picture about how to implement.
So i was just hapless , just before dinner i talked to divyendu who solved this problem then i got a glimpse of where to go. I just coded it from scratch and finally it worked it got accepted but it took 1.32 sec , i know merely less than one and half sec. but from programming point of view it is quite high. Best solution of this problem took merely 0.02 sec ( amazing ) but he took more space almost 32 MB while i took 20 MB.
Our well known saying apply here also , to get something we have to sacrifice something. For better time we have to consume a more space and vice versa .My rank is getting better but i am still in beginning stage , i have a long way to go .................
So i was just hapless , just before dinner i talked to divyendu who solved this problem then i got a glimpse of where to go. I just coded it from scratch and finally it worked it got accepted but it took 1.32 sec , i know merely less than one and half sec. but from programming point of view it is quite high. Best solution of this problem took merely 0.02 sec ( amazing ) but he took more space almost 32 MB while i took 20 MB.
Our well known saying apply here also , to get something we have to sacrifice something. For better time we have to consume a more space and vice versa .My rank is getting better but i am still in beginning stage , i have a long way to go .................
Tuesday, August 26, 2008
Mataki Fhod in IIIT Hyderabad
What the moment that was , i am spending some of best moment of my life in IIIT , i never imagined that i will be part of such an extraordinary place. Two days back we celebrate Krishana Janamastmi here , here we had Matki Phod event which i saw in TV only , as against last year , i took participate this year , it was an amazing experiance.
Last week we had fresher party for our new batches ,another wonderful time . First time in my life i took responsibility for an event , our team did well to make it into successful event. Students here are so exceptional that if one face this crowd then he can face any one anywhere. Hats off to them.
When i see me now as against last year or back , i am getting more and more confident , thanx to shushanta ( my best friend and ex part time student ) for creating a love for programming, it is one of the best human created thing , but some time it gives a lot of frustration as well when i dont get what i wish to.
Last week we had fresher party for our new batches ,another wonderful time . First time in my life i took responsibility for an event , our team did well to make it into successful event. Students here are so exceptional that if one face this crowd then he can face any one anywhere. Hats off to them.
When i see me now as against last year or back , i am getting more and more confident , thanx to shushanta ( my best friend and ex part time student ) for creating a love for programming, it is one of the best human created thing , but some time it gives a lot of frustration as well when i dont get what i wish to.
Wednesday, August 20, 2008
APS back with a Bang!!!!!!!
APS ( Advance Problem Solving ), it is a fearful word to almost all PG student who come here and take this course. It is completely programming oriented subject and students have to go through rigorous schedule , two labs of two hours in a week where we have to solve some programming puzzle on the spot and if we are lucky ( most of times ) we get extra time after lab also.
In short , it consumes entire day , then come a wonderful assignment which takes a lot of time and give immense pressure , but the beauty of this course is that students unintentionally become good programmer, he start to think logically.
It is this course which taught me real programming , but since last week i have become a bit lazy that's why my rank is getting better but the pace is a point of worry.
Today was special as one of wrestler from our country won bronze , it was very pleasant surprise and second indian cricket team anyhow manage to won , but what matter is they won. In this olympic one thing that surpise me most is medal tally , china with 46 gold today is way ahead of USA with 25 odd gold out of which 8 won by its legendry swimmer.
is this the end of monoply of USA in olympic game . Lets see
In short , it consumes entire day , then come a wonderful assignment which takes a lot of time and give immense pressure , but the beauty of this course is that students unintentionally become good programmer, he start to think logically.
It is this course which taught me real programming , but since last week i have become a bit lazy that's why my rank is getting better but the pace is a point of worry.
Today was special as one of wrestler from our country won bronze , it was very pleasant surprise and second indian cricket team anyhow manage to won , but what matter is they won. In this olympic one thing that surpise me most is medal tally , china with 46 gold today is way ahead of USA with 25 odd gold out of which 8 won by its legendry swimmer.
is this the end of monoply of USA in olympic game . Lets see
Friday, August 15, 2008
String of Research Papers
Happy Independence days to all of you. Couple of days back i saw a problem on www.spoj.pl , it is harder version of its ancestor, i tried to use Dynamic Programming , but as was expected it didn't got accepted , every time TLE ( time limit exceeded ) . By this time only merely 28 users were able to solve this problem ( Out of existing more than 6000 ). After that i decided to put some serious time on this problem .
I went through its forum and got some help from there , in short i had to gone through two new algorithm one is Pollard Rho ( for quickly generating divisor of huge numbers near to 10000000000000000 ) and the other one is Miller - Rabin test for primality . I just finished these two ( in fact sadly pollard Rho consumed a lot of time) , now heading for real battle i.e. Coding.
In the mean time no more big news came from indian side in olympic except that Mr. Bindra is flooded with rewards ( that he truly deserves ).
I went through its forum and got some help from there , in short i had to gone through two new algorithm one is Pollard Rho ( for quickly generating divisor of huge numbers near to 10000000000000000 ) and the other one is Miller - Rabin test for primality . I just finished these two ( in fact sadly pollard Rho consumed a lot of time) , now heading for real battle i.e. Coding.
In the mean time no more big news came from indian side in olympic except that Mr. Bindra is flooded with rewards ( that he truly deserves ).
Wednesday, August 13, 2008
ReEntry in 1000
Today was quite special because of one reason, that is i made a reentry into 1000 club on www.spoj.pl. It was quite satisfying to be in that club. But sadly i made a habit of not listening my mind completely, because of this i always commit at least one silly mistake and my program does not get uploaded in one shot.
Yesterday we had an interaction session with our juniors , we had fun but as always i commit a mistake there ( not going to tell ) , but it was quite disappointing from my side :(..................
Yesterday we had an interaction session with our juniors , we had fun but as always i commit a mistake there ( not going to tell ) , but it was quite disappointing from my side :(..................
Monday, August 11, 2008
Good News , Bad News!!!
today i was feeling sleepy since morning, even in class i wasn't able to concentrate completely.after having lunch i went to sleep, ya but before that i just checked latest score of india , they were all out in second inning for 268 and gave sri lanka merely 122 to score for series win. This disappoint me as they are continuously struggling against a new comer mendis. Although sri lanka lost two early wickets but in the end it was that only two wicket that they lost.
So Bad News India lost test series 2-1.
When i wake up i immediately checked my computer , then there i saw Good News of the day .
Abhinav Bindra won Gold in olympic in shooting. what a good news it was India won gold in after so long. three cheers for Abhinav
So Bad News India lost test series 2-1.
When i wake up i immediately checked my computer , then there i saw Good News of the day .
Abhinav Bindra won Gold in olympic in shooting. what a good news it was India won gold in after so long. three cheers for Abhinav
Thursday, July 31, 2008
Brand IIT , M.I. and My Banglore Trip
I just watched M.I. ( Hindi ) , i was waiting for this movie since last one week. In the initial part of the movie i heard a word for which i always fantasize since after passing XII , in fact it is a super word for me "IIT". Mr Vikas Sagar was a topper from IIT .Just after listening this i went into deep thought , Debut of IIT in Bollywood.
This movies was like as i expected , an attempt to copying from couple of Hollywood movies. Here in our college if we caught in involving this kind of activities ( copying, cheating ) we are honoured with F grade , one can imagine if our facuilties are members of Sensor Board , they will save our a lot of time.
Anyways last week i was in Banglore , one of my childhood friend is working there and soon leaving for U.S. for long time. He is from IITK ( again super word ) . While going to his home we were talking about IIIT ( one more I in super word ) and i was telling him that how my college doing grat in research and how students of this college are admired everywhere .
Suddenly he told me " Yaar tum to IIT ki izzat utar dete ho , main bhi IIT se hoon ", he told it so innocently that i couldnt stop myself from laughing . He is one of very best person around me, he is that sort of person who radiate positive energy and crate positive invironment around them. We talked( wrong word again , he told ) about life , setting and achieveing goals in life.
At the same day when i reach in Banglore ,in evening i heard about sequel blast in the city and next day in Ahemdabad. I never understand why this happens , and these things always remind me that life ..............i don't have words .
This movies was like as i expected , an attempt to copying from couple of Hollywood movies. Here in our college if we caught in involving this kind of activities ( copying, cheating ) we are honoured with F grade , one can imagine if our facuilties are members of Sensor Board , they will save our a lot of time.
Anyways last week i was in Banglore , one of my childhood friend is working there and soon leaving for U.S. for long time. He is from IITK ( again super word ) . While going to his home we were talking about IIIT ( one more I in super word ) and i was telling him that how my college doing grat in research and how students of this college are admired everywhere .
Suddenly he told me " Yaar tum to IIT ki izzat utar dete ho , main bhi IIT se hoon ", he told it so innocently that i couldnt stop myself from laughing . He is one of very best person around me, he is that sort of person who radiate positive energy and crate positive invironment around them. We talked( wrong word again , he told ) about life , setting and achieveing goals in life.
At the same day when i reach in Banglore ,in evening i heard about sequel blast in the city and next day in Ahemdabad. I never understand why this happens , and these things always remind me that life ..............i don't have words .
Subscribe to:
Posts (Atom)