Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp
- #include "tools/sieve.h"
- #include <iostream>
- int main()
- {
- std::vector<long> testing{ tools::getSieves(100) };
- for (auto &t : testing)
- {
- std::cout << t << ' ';
- }
- return 0;
- }
- // tools/sieve.h
- #ifndef SIEVE_H
- #define SIEVE_H
- #include <vector>
- namespace tools
- {
- std::vector<long> getSieves(int max);
- };
- #endif // !SIEVE_H
- // tools/sieve.cpp
- #include <vector>
- namespace tools
- {
- std::vector<long> getSieves(int max)
- {
- bool table[max+1];
- for (int x = 0; x <= max; x++)
- table[x] = true;
- for (int i = 2; i*i <= max; i++)
- if (table[i] == true)
- for (int j = i*2; j <= max; j += i)
- table[j] = false;
- std::vector<long> sieves{};
- for (int i = 2; i <= max; i++)
- if (table[i])
- sieves.push_back(i);
- return sieves;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement