I'll start by explaining step by step what I have to do. First I make an array containing N prime numbers that I create like this:
bool isPrime(int n) {
    if(n <= 1) return false;
    for(int i = 2; i * i <= n; i++)
        if(n % i == 0) return false;
    return true;
}
int p[n + d], j = 0;
for(int i = 0; j < n + d; i++) {
    if(isPrime(i)) {
            p[j] = i;
            j++;
    }
}
I then create a target array Q that I'm going to be using for the calculations:
int q[n];
for(int i = 0; i < n; i++) {
    q[i] = p[i] * p[i + d];
}
Now that I have the traget array Q[N] I need to count how many times this equation is true A + B + C + D = E, where {A, B, C, D, E} are items from the array Q and A <= B <= C <= D. I tried this solution, but it's bruteforcing it and for bigger numbers it just takes minutes to calculate, which is not what I want.
int amount = 0;
for(int i = 1; i < n; i++) {
    for(int j = i - 1; j >= 0; j--) {
        for(int k = j; k >= 0; k--) {
            for(int l = k; l >= 0; l--) {
                for(int m = l; m >= 0; m--) {
                    if(q[j] + q[k] + q[l] + q[m] == q[i])
                        amount++;
                }
            }
        }
    }
}
cout << amount << endl;
For example if I input 15 for N and 1 for D the output should be 2 because:
6 + 15 + 323 + 323 = 667
6 + 143 + 221 + 1147 = 1517
But the code has to be optimized enough to calculate fast for N and D up to 2500.
 
     
     
    