Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. // main.cpp
  2. #include "tools/sieve.h"
  3. #include <iostream>
  4.  
  5. int main()
  6. {
  7.     std::vector<long> testing{ tools::getSieves(100) };
  8.     for (auto &t : testing)
  9.     {
  10.         std::cout << t << ' ';
  11.     }
  12.     return 0;
  13. }
  14.  
  15. // tools/sieve.h
  16. #ifndef SIEVE_H
  17. #define SIEVE_H
  18.  
  19. #include <vector>
  20.  
  21. namespace tools
  22. {
  23.     std::vector<long> getSieves(int max);
  24. };
  25.  
  26. #endif // !SIEVE_H
  27.  
  28. // tools/sieve.cpp
  29. #include <vector>
  30.  
  31. namespace tools
  32. {
  33.  
  34.     std::vector<long> getSieves(int max)
  35.     {
  36.         bool table[max+1];
  37.         for (int x = 0; x <= max; x++)
  38.             table[x] = true;
  39.         for (int i = 2; i*i <= max; i++)
  40.             if (table[i] == true)
  41.                 for (int j = i*2; j <= max; j += i)
  42.                     table[j] = false;
  43.         std::vector<long> sieves{};
  44.         for (int i = 2; i <= max; i++)
  45.             if (table[i])
  46.                 sieves.push_back(i);
  47.         return sieves;
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement