Problem 10

2018-08-05  本文已影响0人  guanjianhe
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
#include <stdio.h>

#define NUM 2000000

int main()
{
    int i, j;
    unsigned long int sum = 2;
    char arr[NUM] = { 0 };
    
    for ( i = 3; i < NUM; i+=2 )
    {
        if ( 0 == arr[i] )
        {
            sum += i;
            for ( j = 2; i*j < NUM; j++ )
            {
                arr[i * j] = 1;
            }
        }
    }
    printf( "%ld\n", sum );
    return(0);
}

解出答案是:142913828922

上一篇 下一篇

猜你喜欢

热点阅读