Monthly Archive for April, 2011

Project Euler : Count Integral Shortest Paths In All Cuboid Smaller Than Certain Dimension

Problem 86 of Project Euler is the next problem I took up. Given a cuboid with integral dimensions, the shortest distance for an ant to crawl from one corner to diagonally opposite corner need not be an integral number. The problem statement required one to consider all the possible cuboid up to a maximum size of M x M x M and report the number of cuboid that had integral value for the shortest path.

The question has a huge hint hidden within its wording. It goes on to provide two examples – for M = 99 the count is 1975 and for M  = 100 the count is 2060! Solve the problem incrementally. This problem can be disintegrated into 3 smaller problems.

Firstly, we need to know the calculation of shortest path in a cuboid as an ant would crawl. To do this, explode the 3D figure into a 2D figure keeping track of the corners. It’s a pretty simple matter to calculate the distance now. There’s one thing to be remembered though – the explosion can be done in 3 different ways. So we have potentially 3 candidates for the shortest path.

Given the dimension as a X b X c, the squared distance is one of (a+b)^{2} + c^{2}(b+c)^{2} + a^{2} or (c+a)^{2} + b^{2}. We shall see later that in our case we do not need to perform 3 calculations as the choice becomes quite obvious.

Next  we tackle the problem of induction. Say we know that the count for the problem size  M x M x M is some p. How do we calculate the value for the size (M+1) x (M+1) x (M+1)? If you were to imagine two cuboid of dimensions M and (M+1) and enumerate all the cuboid not covered by the one of dimension M, you would find them to be of the form:

i X j X (M+1)

where i = 1 to M + 1

and j = i to M + 1

There lies our solution! We setup a nested for loop and calculate the shortest distances for each of these extra cuboid. It should now be pretty obvious to notice that for the computation of shortest distance we only need to consider the case (i+j)^{2} + (M+1)^{2}. Thus we can go from M to M+1.

I hope you have noticed some redundancy in the above loop setup. Every time we calculate (i+j). Consider the two cases:

i = 1, j = 3

i = 2, j = 2

In both these cases the value of (i+j) is the same. We are unnecessarily computing values already computed. Being a little cleverer we can improve the current O(n^{2}) to O(n).

Instead of using a nested loop, we use a single for-loop from the range of 2 to 2*(M+1) as those are the range for the sum of i and j. But this introduces another problem. Say for a particular sum we get that the shortest path is an integer. We need to know the number of ways in which the sum can be decomposed into i and j. I setup a single line function to calculate this value and I was done!

The code ran in 258ms on my Lenovo T410 laptop. Below is my function to move from M to M+1. To solve the Project Euler problem, I just needed to call this function in a loop, always updating the memoized value.

 

    /**
     * Count the number of integral shortest paths in all cuboids upto a size of size X size X size
     *
     * @param size the maximum dimensions of the cuboid
     * @param memoizedValue the value for a cuboid with max dimension size - 1
     * @return the value for a cuboid of max dimension size X size X size
     */
    public int countIntegralShortDistances(int size, int memoizedValue) {
        double sizeSquared = Math.pow(size, 2);
        for (int i = 2; i <= 2 * size; i++)
            if (NumberUtil.isInteger(Math.sqrt(Math.pow(i, 2) + sizeSquared))) memoizedValue += NumberUtil
                    .countSummations(i, size);
        return memoizedValue;
    }

Popularity: 14% [?]

eBay Resolution Center Happiness Factor

I recently bought an item on eBay. The seller promised to update the tracking id of the shipment in a weeks time. But even after the week had passed, there was no update. I contacted the seller and he just gave me lame excuses. Patiently, I waited a couple of weeks more for the item to arrive, but nope! I checked the item page again and lo!  - the seller had been de-registered from eBay. Scammed!

I immediately opened up a case with eBay customer support. Their procedure requires one to fill up a form and provide contact number. The form is then forwarded to the seller, who is given a weeks notice to respond. Well, in my case, the seller obviously did not respond. Another week passed by.

After the stipulated time of 7 days, I escalated the issue. When you do this, you transfer the power to the eBay customer support to make a ruling. Within minutes I received a mail telling me that the customer support had ruled in my favor and that the entire amount has been refunded back to my PayPal account. :)

I logged into my paypal account and verified that the amount had been credited. In fact, PayPal had actually transferred the amount to my credit card which was used to make the payment.

Now I trust eBay more. Having gone through this process once, I can be more confident buying items.

Popularity: 4% [?]

My Gmail’s Gotten Too Slow

I love Gmail. I’ve used it as the only mail service since 2005. And thanks to the generous capacity, I’ve never had to delete any conversation. The tons of thousands of chat logs are all archived in my account. And when they rolled out the labels feature, I adopted it pretty quickly and cleaned up my inbox.

I am currently using 1808 MB of space – 23% of their allotted capacity.

But I have a complain now. My gmail’s gotten too slow! It’s not because of network issue. At home I have a 4mbps broadband connection. And yet I have to wait for like half a minute after sign-in action for the inbox to appear. Also when I send a mail, it takes forever. In fact, it has now become a routine for the message – Sending – to appear when I hit the send button.

And apparently, I am not the only one. Read this article at TechCrunch : http://techcrunch.com/2011/04/05/gmail-new-deal/

I know a few seconds or a minute is not going to kill me. But I hate having to stare at empty screen waiting for the action to complete. It’s the user experience which is being effected now.

I can only hope that whatever issue is causing the latency gets resolved soon.

Popularity: 5% [?]

My Wishlist

It’s been a long time since I updated my wish list last. I have got quite a few additions to my possessions and new items have been added to my list. First of all, let me start with what I have bought in the recent time.

My costliest purchase was the iTouch 4th generation! I simply am addicted to music. It gets me high and I needed this device to make me complete. Now I am never without my collection.

I’ve got a great electric razor in Philips Arcitec. Though it is no Braun Series 7, but it is good enough for me. I love the smooth feel after a shave :) . I also have bought myself a Royal Challengers Bangalore Reebok jersey and a Ferrari Puma polo. The RCB jersey I wore when I went to watch the RCB vs. MI match, and the Ferrari polo will come useful when I go to the Indian F1 GP later this year. Oh, and I am totally in love with the Ferrari polo. It’s a black tee-shirt, with a red stripe on the left hand front. The prancing pony is embroidered on the red stripe.

Tomorrow I will purchase Chelsea jersey, finally might I add! The new season jersey is a bore. I prefer the 2010-11 season jersey. The red collar with a tinge of white gives it a beautiful look.

New additions to my list include a mobile phone. At present I have a Sony Xperia X1. It’s an outdated handset and I want to get rid of it. Since I already have an iTouch, I am not too keen on an iPhone, however advanced and cool might it be. I am more inclined towards an Android smart phone. But Android is in the initial stages and I would rather want to wait before making a hasty decision and spending 40K. I want to wait till a phone gets my heart racing and makes it an impulsive decision to buy it.

Another gadget I would like to possess is a Kindle. A colleague of mine has one. I quite liked the look of it. At a first glance you couldn’t say if it was a digital surface or a plain paper with text on it. And for a person like me, who has once again discovered his love for reading, it’s a boon.

I have given up on the laptop. I still do miss Ubuntu dearly, but I have learnt to ignore it. I have taught myself to be content with the windows laptop my company has provided me.

I have now been working for two years and have saved enough money to buy a car, taking very little loan. Also I have gotten quite bored of my bike. But I am not in any hurry to get a car. That can wait. However, I can firmly state that I shall not be buying any other bike. My next vehicle will definitely be a car.

I can’t think of anything more right now. Shall update later!

Popularity: 2% [?]

Chelsea’s Silver Lining

For Chelsea, this season is almost over and done.  They will definitely finish in the top 4 of the league, and with a little effort can be 3. Only the most optimist will bet on their being 2nd but that’ll depend more on Arsenal than us. Coming up next is the Champions League 2nd Leg against Manchester United. ManU have an away goal and a victory and though it pains me to say this but that might just be enough to keep the misfiring Chelsea out.

One may always analyse what went wrong this season. A team that netted record number of goals last season are struggling to put the ball in even in the one-on-one situation! Was it Ray Wilkins? Was it because of Drogba’s malaria and the clubs stupidity to play him then? Was it the injuries to Lampard, Terry, Essien? Alex we have been able to replace with David Luiz who has done exceptionally well, but he is unfortunately cup tied in the Champions League. And not the least of all, is it because of Abramovich’s interference with the running of the club – Mourinho’s fallout with the owner is a classic example of why managers find it difficult at Chelsea.

I am past all that. I am now looking forward to the next season, taking any positives out of this one. And there are indeed a few things to be cheerful about. Let me start with Ramires. The guy runs around on the field so much! He has been the most consistent mid fielder in the second half of the season which is in stark contrast to the first half – remember when Tevez easily won the ball from Ramires and went on to score the only goal of the game. Ramires is a typical Brazilian – stylish with the ball and not much physical in approach. He has already replaces Mikel in the starting line up.

Many will disagree with me when I say that I think Torres will be the future of a great Chelsea attack. His recent performances may not suggest that yet, but they should be considered in the light of the situation. Torres has been out of form for almost a year now. He was out injured at this time of the year last season. He did not play well in the World Cup. Liverpool lacked desire and he was clearly unhappy there. Things have hardly gone the way he would have wanted. Also he is a person who likes to play alone up front. Considering the above, I think he is playing quite well these days. He has definitely got a lot of chances to score goals at Chelsea. He just needs the confidence back and he’ll be on fire. Strikers need that bit of confidence and faith in themselves – the belief that they can put the ball into the net every time they have the ball. He just needs a couple of goals to start with. They won’t come easy and free, but they’ll come.

A lot has already been said about David Luiz. The guy’s performances have been amazing. He has made an instant impact on the field. Going into the next season, who knows he might be the permanent fixture with Terry and Alex being rotated!

I must also add Sturridge to this list. I have been following his since his loan move to Bolton and boy is he scoring goals! The best thing is that he is finishing well. I think he should definitely be in the team next season. Kakuta has not had that big an impact at Fulham, but then he’s younger and will get his chances. And I vote we send McEachran to some Premier League team on loan and get him into the first team as soon as possible. We have a natural replacement for Lampard in him. But Kakuta and McEachran are for the future – perhaps in a couple of years.

One thing we need to do is to refresh our squad a bit. I might be being a bit bold in suggesting that we part ways with Drogba, Kalou and Malouda. With Torres and Sturridge, we are very well covered in that aspect. We could move into a single striker formation which is favoured by Torres. Anelka goes in just behind the striker – a role he played well in a game earlier in the yer, though I cannot remember it now. Midfield manned by Lampard and Essien, the right-wing with Benayoun and Ramires. McEachran can slot in for a few games. We need someone for the left hand side. Malouda’s been suffering and Kakuta is not there yet. In defense, we are very well covered for the CD but we need another good full back. Paulo Ferreira will have to go.

More than strikers, I think it is the midfielders that we need. How I wish to see Chelsea get back to that goal scoring form of last year.

Popularity: 2% [?]