工学1号馆

home

Project Euler 10–Summation of primes

Wu Yudong    November 07, 2017     欧拉计划   507   

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

10以下的质数的和是2 + 3 + 5 + 7 = 17.

找出两百万以下所有质数的和。

#include<stdio.h>
#include<math.h>
#include<stdbool.h>

#define N 2000000

bool prim(int n)
{
	int i;
	for (i = 2; i * i <= n; i++) {
		if (n % i == 0)
			return false;
	}
	return true;
}

int main()
{
	int i;
	long long sum = 2;
	for (i = 3; i <= N; i = i + 2) {
		if (prim(i)) {
			sum += i;
		}
	}
	printf("%lld\n", sum);

	return 0;
}

 Answer:142913828922

Completed on Tue, 23 Jul 2013, 17:02

如果文章对您有帮助,欢迎点击下方按钮打赏作者

Comments

No comments yet.
To verify that you are human, please fill in "七"(required)