美文网首页
Problem 10

Problem 10

作者: guanjianhe | 来源:发表于2018-08-05 22:54 被阅读0次
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

相关文章

网友评论

      本文标题:Problem 10

      本文链接:https://www.haomeiwen.com/subject/fppivftx.html