<p>Given an integer array <code>nums</code>, return <em>the sum of divisors of the integers in that array that have exactly four divisors</em>. If there is no such integer in the array, return <code>0</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [21,4,7]
<strong>Output:</strong> 32
<strong>Explanation:</strong> 
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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [21,21]
<strong>Output:</strong> 64
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

from math import sqrt, ceil
from typing import Any


class Solution:
    def __init__(self, *args, **kwargs):
        self.__primes: set[int] = set([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41])
        self.__composites: set[int] = set([1])
        self.__cache: dict[int, int] = {}
        super().__init__(*args, **kwargs)

    def is_prime(self, n: int) -> bool:
        if n in self.__primes:
            return True
        if n in self.__composites:
            return False
        for i in range(2, ceil(sqrt(n)) + 1):
            if self.is_prime(i) and n % i == 0:
                self.__composites.add(n)
                return False
        self.__primes.add(n)
        return True
    
    def getSum(self, n: int) -> int:
        if n in self.__cache:
            return self.__cache[n]
        if n in self.__primes:
            return 0
        for p in range(2, ceil(sqrt(n)) + 1):
            if n % p == 0:
                q = n // p
                if q == p * p:
                    s = 1 + p + q + n
                    self.__cache[n] = s
                    return s
                elif q != p and self.is_prime(q):
                    s = 1 + p + q + n
                    self.__cache[n] = s
                    return s
                break
        return 0

    def sumFourDivisors(self, nums: List[int]) -> int:
        count = 0
        for num in nums:
            count += self.getSum(num)
        return count

        
18600