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
.
Example 1:
Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.
Constraints:
1 <= nums.length <= 10^4
1 <= nums[i] <= 10^5
struct Solution;
impl Solution {
fn sum_four_divisors(nums: Vec<i32>) -> i32 {
let mut res = 0;
'outer: for x in nums {
let mut i = 2;
let mut v = vec![];
while i * i <= x {
if x % i == 0 {
v.push(i);
if v.len() > 1 {
continue 'outer;
}
}
i += 1;
}
if v.len() == 1 && v[0] * v[0] != x {
res += 1 + v[0] + x / v[0] + x;
}
}
res
}
}
#[test]
fn test() {
let nums = vec![21, 4, 7];
let res = 32;
assert_eq!(Solution::sum_four_divisors(nums), res);
let nums = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let res = 45;
assert_eq!(Solution::sum_four_divisors(nums), res);
}