Leena Lavanya

January 28, 2019

How Many Pairs of Perfect Squares? - Facebook Hacker Cup 2011 Qualification Round

This finds, given an integer, the number of pairs of perfect squares whose sum equal that number. t is the array of integers for which we will run the program.

var t = [];

function number_of_square_pairs(t) {
    var c = 0;
    if (t == 0 || t == 1) c = 1;
    else {
        for (var j = 0; j < Math.pow(t / 2, 0.5); j++) {
            var v = Math.pow((t - j * j), 0.5);
            if (v == Math.round(v)) c++;
        }
    }
    return c;
}

function main() {
    var n = t.length;
    for (var i = 0; i < n; i++) {
        console.log("Case #" + (i + 1) + ": " + number_of_square_pairs(t[i]));
    }
}
© Copyright 2021 Leena Lavanya. Icons by Font Awesome. Image Credit: Taras Shypka on Unsplash.