Advertisement
Taraxacum

sieve of Eratosthenes

Oct 28th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3.  
  4. const size_t maxsize = 2000000;
  5.  
  6. int main(int argc, char const *argv[]) {
  7.     bool arr[maxsize] = { 0 };
  8.     size_t max;
  9.  
  10.     scanf("%d", &max);
  11.     double sup = sqrt(max) + 1;
  12.  
  13.     for (size_t i = 3; i <= sup; i += 2) {
  14.         // printf("i = %d flag = %d\n", i, arr[i]);
  15.         if (arr[i]) {
  16.             continue;
  17.         } else {
  18.             for (size_t j = 3; i * j <= max; j+=2) {
  19.                 arr[i * j] = true;
  20.             }
  21.         }
  22.     }
  23.  
  24.     printf("2");
  25.     for (size_t i = 3; i <= max; i += 2) {
  26.         if (!arr[i]) {
  27.             printf(" %d", i);
  28.         }
  29.     }
  30.  
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement