JavaScript Coding Challenge: "Integers: Recreation One"

JCC Integers: Recreation One

In this article we are going to go over another problem from Codewars.

Problem description

Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42. These divisors squared are: 1, 4, 9, 36, 49, 196, 441, 1764. The sum of the squared divisors is 2500 which is 50 * 50, a square!

Given two integers m, n (1 <= m <= n) we want to find all integers between m and n whose sum of squared divisors is itself a square. 42 is such a number.

The result will be an array of arrays, each subarray having two elements, first the number whose squared divisors is a square and then the sum of the squared divisors.

Examples:

list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]

list_squared(42, 250) --> [[42, 2500], [246, 84100]]

Note: Before moving on to see the Solution, please make sure you write your own solution first. This is the best way for you to learn as much as possible! 🙂

Solution

Although the problem might seem a little bit complex (this is because it has a little more explanations), we'll break it down to understand what it requires:

  1. go over all the numbers from m to n;
  2. find all the divisors of the current number (i);
  3. square all the divisors and add them up;
  4. check if the resulting sum is a square number and if it is, store the number (i) and the sum into an array;

Let's go through all those steps. 😄

Step 1. Loop over the numbers

We'll create a function which will take those two numbers (m and n) as parameters, and we'll create an array (res) which will store the final results (we'll get there in a bit, hold on 😉):

function listSquared(m, n) {
    const res = [];

    for (let i = m; i <= n; i++) {
        // Do the magic
    }

    return res;
}

Step 2. Get all the divisors

In order for us to simplify the code, we're going to create a new function to get all the divisors of a number.

The modulo operation to the rescue as it will return the reminder after division of the two numbers. If this result is 0 it means that it divides exactly and that j (in our case, see below) is a divisor of x.

function divisorsOf(x) {
    const divs = [];

    for (let j = 1; j <= x; j++) {
        if (x % j === 0) {
            divs.push(j);
        }
    }

    return divs;
}

Next, let's add this to our main function:

function listSquared(m, n) {
    const res = [];

    for (let i = m; i <= n; i++) {
        const divs = divisorsOf(i);
    }

    return res;
}

Step 3. Square the divisors and add them up

We're going to use the reduce function to add up all the squares into an accumulator which will return the result and save it into the sumOfSquaredDivs variable:

function listSquared(m, n) {
    const res = [];

    for (let i = m; i <= n; i++) {
        const divs = divisorsOf(i);

        const sumOfSquaredDivs = divs.reduce(
            (accumulator, current) => (accumulator += current * current),
            0
        );
    }

    return res;
}

Step 4. Check if the sum is a Square

Let's write another simple function which will help us find out if a number is a square.

In order to do that, we'll get the number's square root and we'll check if the reminder of the division by 1 is 0. If it is, that means that the square root is an integer.

function isSquare(x) {
    return Math.sqrt(x) % 1 === 0;
}

Great! 😃 Now let's use it:

function listSquared(m, n) {
    const res = [];

    for (let i = m; i <= n; i++) {
        const divs = divisorsOf(i);

        const sumOfSquaredDivs = divs.reduce(
            (accumulator, current) => (accumulator += current * current),
            0
        );

        if (isSquare(sumOfSquaredDivs)) {
            res.push([i, sumOfSquaredDivs]);
        }
    }

    return res;
}

And because this is what the solution requires, we're going to add both the number (i) and the sum into an array and we'll return it.

That's it! Congratulations for making it this far! 😉

Conclusion

Maybe you can find a better, faster, stronger way to solve this problem. Let me know how you did it! 😄

Happy Coding! 😇

Tagged with javascript, challenge, medium, codewars, array, reduce, math, es6