Project Euler #10: Summation of primes(sieve of Eratosthenes)
程序员文章站
2022-03-13 16:52:41
...
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;
}
上一篇: Day01
下一篇: php开发工程师需要掌握哪些技能