Skip to content

Instantly share code, notes, and snippets.

@NixImagery
Created June 30, 2017 07:50
Show Gist options
  • Save NixImagery/5c07f310763577efa6ae4fe98eb16d13 to your computer and use it in GitHub Desktop.
Save NixImagery/5c07f310763577efa6ae4fe98eb16d13 to your computer and use it in GitHub Desktop.
using namespace std;
int main(){
int t;
cin >> t;
// Using sieve of Eratosthenes, from Wikipedia
bool primes[1000001]; // Let A be an array of Boolean values,
fill_n(primes, 1000001, true); // indexed by integers 2 to n, initially all set to true.
for (long i = 2; i <= 1000; ++i) // i = 2, 3, 4, ..., not exceeding root n:
if (primes[i]) // if A[i] is true:
for (long long j = i*i; j <= 1000000; j += i) // for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n:
primes[j] = false; // A[j] := false.
long long answers[t];
long questions[t];
for(int a0 = 0; a0 < t; a0++){
long n;
cin >> n;
questions[a0] = n;
long istart = 2;
long long sum = 0;
for(int l = 0; l < a0; ++l){
if (questions[l] < n) {
sum = answers[l];
istart = questions[l] + 1;
}
}
for (long x = istart; x <= n; ++x)
sum += primes[x] ? x : 0 ;
answers[a0] = sum;
cout << sum << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment