# ZeroJudge - a626: 6. Prime Directive ### 題目連結:https://zerojudge.tw/ShowProblem?problemid=a626 ###### tags: `ZeroJudge` `數學` `數論` `質數` ```cpp= #include <iostream> #include <iomanip> using namespace std; #define SIZE 1050 int primes[SIZE], primeAmount; bool notPrime[SIZE] = { true, true }; const static auto Initialize = []{ cin.sync_with_stdio(false); cin.tie(nullptr); for (int i = 2; i < SIZE; ++i) { if (!notPrime[i]) { primes[primeAmount] = i; ++primeAmount; } for (int j = 0; i * primes[j] < SIZE; ++j) { notPrime[i * primes[j]] = true; if (!(i % primes[j])) break; } } return nullptr; }(); int main() { int upperBound, counts; while (cin >> upperBound) { counts = 0; for (int i = 0; primes[i] <= upperBound; ++i) { cout << setw(10) << primes[i]; ++counts; if (counts == 5) { cout << '\n'; counts = 0; } } if (counts) cout << '\n'; } } ```