Advertisement
allia

решето эратосфена

Oct 15th, 2020
1,961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. void eratosfen (int *arr, int n)
  6. {
  7.   int p = 0;
  8.  
  9.   for (int p = 2; p < n + 1; p++)
  10.   {
  11.     if (arr[p] != 0)
  12.     {
  13.       cout << arr[p] << " ";
  14.       for (int j = p*p; j < n + 1; j += p)
  15.         arr[j] = 0;
  16.     }
  17.   }
  18. }
  19.  
  20. int main()
  21. {
  22.   int n = 0;
  23.   cin >> n;
  24.  
  25.   int *arr = new int[n+1];
  26.  
  27.   for (int i = 0; i < n + 1; i++)
  28.   {
  29.       arr[i] = i;
  30.      cout << arr[i] << " ";
  31.    }
  32.   cout << endl;
  33.  
  34.   eratosfen(arr, n+1);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement