Advertisement
Kolyach

Эратосфен

Dec 9th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.38 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     int n;
  8.     cout << "n= ";
  9.     cin >> n;
  10.     int *a = new int[n + 1];
  11.     int i;
  12.     for (i = 0; i < n + 1; i++)
  13.         a[i] = i;
  14.     for (int p = 2; p < n + 1; p++)
  15.     {
  16.         if (a[p] != 0)
  17.         {
  18.             for (int j = p * p; j < n + 1; j=j+p)
  19.                 a[j] = 0;
  20.         }
  21.     }
  22.     for (i = 0;i < n + 1;i++)
  23.         if (a[i] != 0) cout << a[i] << " ";
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement