Algorithm to improve the sum of prime numbers

/* The description
  of the algorithm to improve the summation of prime numbers     Input
a natural number n, find the sum of prime numbers less than or equal to n Sample input 2 sample output 2 Data size and convention   Test sample guarantee 2 <= n <= 2,000,000 */ #include < stdio.h> #include <stdlib.h> #include <stdbool.h> #define BSSS (0) void input( int [] , int ); void sc( int [] , int ); long long q_sum( int [ ] , int ); int main(void) { int n; scanf("%d", &n ); int * a = (int *)calloc( 2000000 , sizeof(int) ) ; input( a , n ); sc ( a , n ); printf("%lld\n", q_sum( a , n ) ); free(a); return 0; }


































long long q_sum( int a[] , int n )
{
long long sum = 0 ;
int i ;
for( i = 0 ; i < n ; i ++ )
{
if( a[i] != BSSS )
{
sum += a[i] ;
}
}
return sum ;
}


void sc( int a[] , int n )
{
int i;
for( i = 0 ; i < n ; i ++ )
{
if( a[i] != BSSS )
{
int bs;
for( bs = i + a[i] ; bs < n ; bs += a[i] )
{
a[bs] = BSSS ;
}
}
}
}


void input( int a[] , int n )
{
int i;
for( i = 0 ; i < n ; i ++ )
{
a[i] += i + 1 ;
}
a[0] = BSSS ;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324686619&siteId=291194637