Created
February 11, 2026 01:11
-
-
Save dgodfrey206/e0a0baf85c1e189b5366c5d06fa95d0d to your computer and use it in GitHub Desktop.
Find the sum of all the primes below two million.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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