-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1390-four-divisors.js
More file actions
36 lines (33 loc) · 841 Bytes
/
1390-four-divisors.js
File metadata and controls
36 lines (33 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* 1390. Four Divisors
* https://leetcode.com/problems/four-divisors/
* Difficulty: Medium
*
* Given an integer array nums, return the sum of divisors of the integers in that array
* that have exactly four divisors. If there is no such integer in the array, return 0.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var sumFourDivisors = function(nums) {
function countDivisors(num) {
const divisors = new Set();
for (let i = 1; i * i <= num; i++) {
if (num % i === 0) {
divisors.add(i);
divisors.add(num / i);
}
}
return divisors;
}
let result = 0;
for (const num of nums) {
const divisors = countDivisors(num);
if (divisors.size === 4) {
const sum = [...divisors].reduce((acc, val) => acc + val, 0);
result += sum;
}
}
return result;
};