Skip to content

Commit 16c6792

Browse files
committed
Create 1390.四因数.js
1 parent f944b37 commit 16c6792

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

1390.四因数.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
逐个判断每个数字是否满足仅有 4 个因数, 满足就将因数和加到 result
3+
因为 1 和自身是 2 个因数, 如果从 2 到 sqrt(n) 只有 2 个因数就是满足条件的, 注意如果 i * i == n 则只新增一个因数, 否则新增 i 和 n / i 两个因数
4+
*/
5+
6+
/**
7+
* @param {number[]} nums
8+
* @return {number}
9+
*/
10+
var sumFourDivisors = function(nums) {
11+
let result = 0;
12+
for(const n of nums) {
13+
let t = [];
14+
for (let i = 2; i <= Math.sqrt(n); i++) {
15+
if (n % i === 0) {
16+
t.push(i);
17+
if (i * i !== n) {
18+
t.push(n / i);
19+
}
20+
}
21+
if (t.length > 2) {
22+
break;
23+
}
24+
}
25+
if (t.length === 2) {
26+
result += t[0] + t[1] + 1 + n;
27+
}
28+
}
29+
return result;
30+
};

0 commit comments

Comments
 (0)