Post

1284 Four Divisors

1284 Four Divisors

Four Divisors image

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:

1
2
3
4
5
6
7
8
9
**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.

Example 2:

1
2
3
4
**Input:** nums = [21,21]
**Output:** 64

Example 3:

1
2
3
4
**Input:** nums = [1,2,3,4,5]
**Output:** 0

 

Constraints:

1
2
1 <= nums.length <= 104
1 <= nums[i] <= 105
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

class Solution:
    def get_divisors(self, n):

        divisors = []
        limit = n ** 0.5
        i = 1
        while(i <= limit):

            if(n % i == 0):
                divisors.append(i)
                
                poss_div = n//i
                if(poss_div != i):
                    divisors.append(poss_div)

            i += 1
        
        return divisors
    
    def sumFourDivisors(self, nums: list[int]) -> int:
        
        ans = 0
        for num in nums:
            divisors = self.get_divisors(num)

            if(len(divisors) == 4):
                ans += sum(divisors)
        
        return ans
        



This post is licensed under CC BY 4.0 by the author.