fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 1e2 + 1; // 100 + 1
  4. bool not_prime[N]; // (true if it is not prime, false if it is)
  5. vector<int> primes;
  6. void sieve() {
  7. not_prime[0] = not_prime[1] = true;
  8. for (int i = 2; i < N; i += (i == 2 ? 1 : 2)) { // (i == 2 ? 1 : 2) Can you tell me what this is?
  9. if (not_prime[i] == true) continue;
  10. primes.push_back(i); // new prime
  11. for (long long j = i * i; j < N; j += i) { // mark all multiples of i
  12. not_prime[j] = true;
  13. }
  14. }
  15. }
  16. int main() {
  17. sieve();
  18. cout << "All primes in range [1, " << N << "):\n";
  19. for (int i = 0; i < (int)primes.size(); ++i)
  20. cout << primes[i] << " \n"[i + 1 == primes.size()];
  21. return 0;
  22. }
Success #stdin #stdout 0.01s 5404KB
stdin
Standard input is empty
stdout
All primes in range [1, 101):
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97