Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Created February 11, 2026 01:11
Show Gist options
  • Select an option

  • Save dgodfrey206/e0a0baf85c1e189b5366c5d06fa95d0d to your computer and use it in GitHub Desktop.

Select an option

Save dgodfrey206/e0a0baf85c1e189b5366c5d06fa95d0d to your computer and use it in GitHub Desktop.
Find the sum of all the primes below two million.
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
int main() {
int n = 2e6;
long long sum = 2;
for (int i = 3; i < n; i += 2) {
bool prime = true;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
prime = false;
break;
}
}
if (prime) {
sum += i;
}
}
cout << sum << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment