Advertisement
Guest User

dsa

a guest
Feb 17th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int index = 1;
  6. int* eratosthenesArray;
  7. const int sizeOfArray = 20;
  8.  
  9.  
  10.  
  11. void Init()
  12. {
  13.     eratosthenesArray = new int[sizeOfArray];
  14.  
  15.     for (int i = 0; i < sizeOfArray; i++)
  16.     {
  17.         eratosthenesArray[i] = i + 1;
  18.     }
  19. }
  20.  
  21. void EratosthenesAlgorithm()
  22. {
  23.  
  24.     for (int i = index; i < sizeOfArray; i++)
  25.     {
  26.         if (index != i &&
  27.             eratosthenesArray[index] != 0 &&
  28.             eratosthenesArray[i] % eratosthenesArray[index] == 0)
  29.         {
  30.             eratosthenesArray[i] = 0;
  31.         }
  32.     }
  33.     index++;
  34.  
  35.     /*if (index < sizeOfArray)
  36.     {
  37.         EratosthenesAlgorithm();
  38.     }*/
  39. }
  40.  
  41. void Print()
  42. {
  43.     for (int i = 0; i < sizeOfArray; i++)
  44.     {
  45.         cout << eratosthenesArray[i] << " ";
  46.     }
  47.     cout << endl;
  48. }
  49.  
  50. int main()
  51. {
  52.     Init();
  53.     Print();
  54.  
  55.     while (true)
  56.     {
  57.         if (index >= sizeOfArray)
  58.         {
  59.             break;
  60.         }
  61.  
  62.         EratosthenesAlgorithm();
  63.     }
  64.  
  65.     Print();
  66.  
  67.     delete[]eratosthenesArray;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement