I am trying to find the nth( n <= 2000000) cuadrado libre semi prime. I have the following code to do so.

`````` int k = 0;
for(int i = 0; i <= 1000; i++)
{
for(int j = i +1 ; j <= 2500; j++ )
{
semiprimes[k++] = (primes[i]*primes[j]);
}
}
sort(semiprimes,semiprimes+k);
``````

`primes[]` is a list of primes.

My problem is, i get different values for `n = 2000000`, with different limits on the for loops. Could someone tell a way to correctly calculate these limits?

Gracias de antemano ..

preguntado el 03 de mayo de 12 a las 19:05

Pista: el `n`th semiprime must be larger than `n`. Si `p` is the largest prime less than `n/2`, entonces `2p` is a semiprime that should be included in your count. -

@ Jeffrey Sax Could you please elaborate? I still have not figured it out! Keep taking the wrong limits. -

## 2 Respuestas

You want to calculate the nth en el primer semi-prime square-free numbers. "primero" means that you have to generate all of them under a certain value. Your method consist of generating a lot of those numbers, sort them and extract the nth first values.

This can be a good approach but you must have all the numbers generated. Having two different limits in your nested loops is a good way to miss some of them (in your example, you are not calculating `primes[1001]*primes[1002]` que debería estar en `semiprimes`).

To avoid this problem, you have to compute all the semi-prime numbers in a square, say `[1,L]*[1,L]`, where L is your limit for both loops.

To determine L, all you need is it to count. Let N be the number of semi-prime square-free numbers under `primes[L-1]*primes[L-1]`.

`N = (L * L - L) / 2`

L*L is the total number of pairwise multiplications. L is the number of squares. This has two be divided by two to get the right number (because `primes[i]*primes[j] = primes[j]*primes[i]`).

You want to pick L such that n<=N. So for n = 2000000 :

``````    int L = 2001, k = 0;
for(int i = 0; i < L; i++)
{
for(int j = i+1 ; j < L; j++ )
{
semiprimes[k++] = (primes[i]*primes[j]);
}
}
sort(semiprimes,semiprimes+k);
``````

contestado el 24 de mayo de 12 a las 15:05

This value of L is wrong! See Jeffrey Sax's comment above. You will be missing semiprimes that are of the type 2*p where p is a prime far beyond primes[2000]. - erick wong

I don't believe an approach that works by computing all semiprimes inside a box will work in any reasonable amount of time. Say we graph the factors (p,q) of the first 2 million semiprimes. To make the graph more symmetric, let's plot a point for both (p,q) and (q,p). The graph does not form a nice rectangular region, but instead looks more like the hyperbola y=1/x. This hyperbola stretches out quite far, and iterating over the entire rectangle containing these will be a lot of wasted computation.

You may want to consider first solving the problem "how many semiprimes are there below N?" and then using a binary search. Each query can be done in about sqrt(N) steps (hint: binary search strikes again). You will need a fairly large table of primes, certainly at least up to 1 million, probably more. Although this can be trimmed down by an arbitrarily large constant factor with some precomputation.

Respondido el 14 de junio de 12 a las 05:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.