Wednesday, January 12, 2011

Facebook Hackers Cup eliminations

Last weekend I was participating in the elimination part of Facebook Hacker Cup. There were three problems to solve and it was obligatory to finish at least one. Since it was last weekend before Mozilla GameOn 2010 deadline, I had no time for solving academical, non-real life, algorithmic problems I was always poor in. So I just took a look to the first one [mirror]. It looked quite easy, and in couple of minutes I had kind of prototype algorithm implemented in pure Javascript. Main idea of the problem was that the biggest number which square could be part of the input, cannot be bigger than square root of the input divided by two. So I simply iterate from 1 to that max, and in each iteration I checked square root of input number reduced by square of 'i'. If it was integer, we have sum of two squares. If float, we haven't. Piece of cake:). My Javascript implemetation in Rhino (as I described before):
var check = function(input) {
    var result = 0,
        max = Math.sqrt(input/2);

    if(Math.sqrt(input)%1==0) {
        result++;
    }

    for(i=1;i <= max;i++) {
        if(Math.sqrt((input-Math.pow(i, 2)))%1==0) {
            result++;
        }
    }
    return result;
}

var MainFunction = function(args) {
    var a = readFile(args).split("\n");

    for (var i=1, j=a.length;i < j ;i++) {
        print(check(a[i]));
    }
}(arguments);

1 comment:

  1. This is where Facebook Hacker Pro comes in! This unique software allows users to hack into any Facebook account, even if the password is not known. The best part is that the user does not even require the username to access the profile. All that is needed is the public Facebook profile link and the account will be accessible within minutes.

    This is the best facebook hacking software that launches in mere seconds and no additional settings are needed. There is no need to call in a professional hacker nor do you need to spend hours with a shady online password recovery tool.

    The Facebook Password Hacker Pro saves everyone’s time and money.

    ReplyDelete