<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>

class Solution {
public:
    vector<int> generatePrimes(int limit) {
        vector<bool> isPrime(limit + 1, true);
        isPrime[0] = isPrime[1] = false;

        for (int i = 2; i * i <= limit; ++i) {
            if (isPrime[i]) {
                for (int j = i * i; j <= limit; j += i) {
                    isPrime[j] = false;
                }
            }
        }

        vector<int> primes;
        for (int i = 2; i <= limit; ++i) {
            if (isPrime[i]) {
                primes.push_back(i);
            }
        }

        return primes;
    }
    int sumFourDivisors(vector<int>& nums) {
        vector<int> v = generatePrimes(100006);
        int summ = 0;
        for (int i = 0; i < nums.size(); i++) {
            int begg = 0;
            bool beggFound = false;
            int cntBegg = 0;
            int endd = 0;
            int orginalVal =  nums[i];
            while (begg < v.size()) {
                if (nums[i] % v[begg]) {
                    begg++;
                } else {
                    beggFound = true;
                    while (nums[i] % v[begg] == 0) {
                        nums[i] = nums[i] / v[begg];
                        cntBegg++;
                    }
                    break;
                }
            }

            int cntEndd = 0;

            if (beggFound) {
                endd = begg + 1;
                while (endd < v.size()) {
                    if (nums[i] % v[endd] == 0) {
                        while (nums[i] % v[endd] == 0) {
                            nums[i] = nums[i] / v[endd];
                            cntEndd++;
                        }
                        break;
                    } else {
                        endd++;
                    }
                }
            }

            if (nums[i] != 1) {
                continue;
            }

            cout<<orginalVal<< " "<<cntBegg<<" "<<cntEndd<<endl;

            if (cntBegg == 1 && cntEndd == 1) {
                summ = summ + 1 + orginalVal + v[begg] + v[endd];
            } else if (cntBegg == 3 && cntEndd == 0) {
                summ = summ + 1 + orginalVal + v[begg] + (v[begg] * v[begg]);
            }
        }
        return summ;
    }
};
17900