2
I’m making a question that the number of elements in the vector are in 10 6 and just wanted to copy certain element to it only that I did the normal way, but common, there is another possibility if make the fastest copy?
for(i = 0; i < teste; i++)
{
if(primo(vetor[i]))
{
aux[cont] = vetor[i];
cont++;
}
}
Algorithm to find the prime number
int primo(int num)
{
int pri = 1, i, raiz = sqrt(num);
if(num == 2)
{
return 1;
}
if (num == 1 || num == 0 || num % 2 == 0)
{
return 0;
}
for(i = 3; i <= raiz; i += 2)
{
if(num % i == 0)
{
pri = 0;
break;
}
}
return pri;
}
The problem is probably not in the copy but in the calculation of the primes. One way to make a very fast calculation of the primes is by using the algorithm of Sieve of Eratosthenes, which is suitable for calculating primes up to a certain limit number.
– Isac