- Related Questions & Answers
- Squared and square rooted sum of numbers of an array in JavaScript
- Check for perfect square in JavaScript
- Squared sum of n odd numbers - JavaScript
- Difference between sum of square and square of sum in JavaScript
- Building an array of specific size with consecutive element sum being perfect square in JavaScript
- Can we have integers as elements of an enum in Java?
- Count all perfect divisors of a number in C++
- Check Perfect Square or Not
- Divisors of n-square that are not divisors of n in C++ Program
- Count of pairs in an array whose sum is a perfect square in C++
- Print n numbers such that their sum is a perfect square
- Minimum number of Square Free Divisors in C++
- C Program to find sum of perfect square elements in an array using pointers.
- Check for perfect square without using Math libraries - JavaScript
- Program to count number of permutations where sum of adjacent pairs are perfect square in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are required to write a JavaScript function that takes in a range specified by an array of two numbers m and n.

Our function is supposed to find all integers between m and n (m and n integers such as 1 <= m <= n) such that the sum of their squared divisors is itself a square.

It should return an array of subarrays. The subarrays will have two elements: first the number the squared divisors of which is a square and then the sum of the squared divisors.

Following is the code −

const range = [1, 500]; const listSquared = ([m, n]) => { const res = []; for (let i = m; i <= n; ++i) { let sum = getDivisors(i).reduce((sum, n) => sum + n * n, 0); let ok = Number.isInteger(Math.sqrt(sum)); if (ok) { res.push([i, sum]); } } return res; } function getDivisors (n) { const divisors = []; for (let i = 1; i <= n / 2; ++i) { if (n % i) { continue; } divisors.push(i); } return divisors.concat([n]); } console.log(listSquared(range));

[ [ 1, 1 ], [ 42, 2500 ], [ 246, 84100 ], [ 287, 84100 ] ]

Advertisements