Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Integers have sum of squared divisors as perfect square in JavaScript
Problem
We need to write a JavaScript function that takes a range specified by two numbers m and n, and finds all integers between m and n where the sum of their squared divisors is itself a perfect square.
The function should return an array of subarrays, where each subarray contains the number and the sum of its squared divisors.
Understanding the Concept
For a number to qualify, we need to:
- Find all divisors of the number
- Square each divisor and sum them up
- Check if this sum is a perfect square
For example, the number 42 has divisors [1, 2, 3, 6, 7, 14, 21, 42]. The sum of squared divisors is 1² + 2² + 3² + 6² + 7² + 14² + 21² + 42² = 2500, and ?2500 = 50, which is a perfect square.
Solution
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 === 0) {
divisors.push(i);
}
}
return divisors.concat([n]);
}
console.log(listSquared(range));
Output
[ [ 1, 1 ], [ 42, 2500 ], [ 246, 84100 ], [ 287, 84100 ] ]
Step-by-Step Breakdown
Let's trace through how this works for number 42:
// Example: Finding squared divisors for 42
function demonstrateFor42() {
const num = 42;
const divisors = getDivisors(num);
console.log("Divisors of 42:", divisors);
const squaredSum = divisors.reduce((sum, divisor) => {
console.log(`${divisor}² = ${divisor * divisor}`);
return sum + divisor * divisor;
}, 0);
console.log("Sum of squared divisors:", squaredSum);
console.log("Square root of sum:", Math.sqrt(squaredSum));
console.log("Is perfect square?", Number.isInteger(Math.sqrt(squaredSum)));
}
function getDivisors(n) {
const divisors = [];
for (let i = 1; i <= n / 2; ++i) {
if (n % i === 0) {
divisors.push(i);
}
}
return divisors.concat([n]);
}
demonstrateFor42();
Optimized Version
Here's a more efficient approach that finds divisors in O(?n) time:
const listSquaredOptimized = ([m, n]) => {
const res = [];
for (let i = m; i <= n; i++) {
let sumSquared = 0;
// Find divisors more efficiently
for (let j = 1; j * j <= i; j++) {
if (i % j === 0) {
sumSquared += j * j;
// Add the paired divisor if it's different
if (j !== i / j) {
sumSquared += (i / j) * (i / j);
}
}
}
if (Number.isInteger(Math.sqrt(sumSquared))) {
res.push([i, sumSquared]);
}
}
return res;
};
console.log("Optimized result:", listSquaredOptimized([1, 100]));
Key Points
- The algorithm checks each number in the range individually
- For each number, it finds all divisors and squares them
- It uses
Number.isInteger(Math.sqrt(sum))to check if the sum is a perfect square - The optimized version reduces time complexity from O(n) to O(?n) for finding divisors
Conclusion
This solution efficiently finds integers whose sum of squared divisors forms a perfect square. The optimized version improves performance by finding divisors in O(?n) time rather than checking every number up to n/2.
