欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Project Euler #10: Summation of primes(sieve of Eratosthenes)

程序员文章站 2022-03-13 16:52:41
...

Project Euler #10: Summation of primes(sieve of Eratosthenes)
HackerRank:https://www.hackerrank.com/contests/projecteuler/challenges/euler010/problem

#include <bits/stdc++.h>
using namespace std;

bool prime[2000001];
vector<int> arr;
void seive(){
	for (int i = 2; i < 1000001; ++i){
		if (prime[i]){
			arr.push_back(i);
			for (int j = i + i; j < 1000001; j += i)prime[j] = false;
		}
	}
}

int main()
{
	fill(prime, prime + 1000001, true);
	prime[0] = prime[1] = false;
	seive();
	int t, n;
	scanf("%d", &t);
	while (t--){
		long long sum = 0;
		scanf("%d", &n);
		for (int i = 0; arr[i] <= n && i < arr.size(); ++i)sum += arr[i];
		printf("%lld\n", sum);
	}
	return 0;
}
相关标签: 数论