On Friday 15 July 2011 16:21:51 Wladimir Tavares wrote:
> Does anyone have any tips for this problem?
>
> My code is time limit exceeded
> #define MAX 86028122
> bitset <MAX> primo;
> long long int lista[5000001];
> long long int cont;
>
> void crivo(){
>
> long long int i,j;
> long long int limite;
> for(i=2;i<MAX;i++) primo.set(i,1);
> for(i=4;i<MAX;i=i+2) primo.set(i,0);
> lista[0]=2;
> cont=1;
> limite = sqrt(MAX);
> for(i=3;i<=limite;i=i+2){
> if(primo.test(i)){
> lista[cont++]=i;
> for(j=i*i;j<MAX;j=j+i) primo.set(j,0) ;
> }
> }
>
> for(i=limite+1;i<MAX;i++)
> if(primo[i]==1) lista[cont++]=i;
>
> }
>
> Wladimir Araujo Tavares
> *Federal University of CearĂ¡
>
> *
This help?
#define TRUE 1
#define FALSE 0
int is_prime(int a)
{
int d;
if (a == 2)
return TRUE;
else if (a % 2 == 0)
return FALSE;
else {
for (d = 3; d <= sqrt(a); d += 2) {
if (a % d == 0)
return FALSE;
}
return TRUE;
}
}
--
Felipe Ferreri Tonello
[email protected]
felipetonello.com
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.