Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Created February 10, 2026 17:13
Show Gist options
  • Select an option

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

Select an option

Save dgodfrey206/ea359bce2f7c822f425e509a8a8bf738 to your computer and use it in GitHub Desktop.
What is the 10001st prime number?
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
int main() {
int n = 10001;
int limit = n/2;
vector<int> prime;
while (true) {
limit *= 2;
prime.resize(limit + 1, 1);
for (int i = 2; i * i <= limit; i++) {
if (prime[i]) {
for (int k = i * i; k <= limit; k += i) {
prime[k] = 0;
}
}
}
int lastPrime = 1;
int count = 0;
for (int i = 2; i <= limit; i++) {
if (prime[i]) {
count++;
lastPrime = i;
}
if (count == n) {
break;
}
}
if (count == n) {
cout << lastPrime << '\n';
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment