#include "sieve.hh" #include #include [[nodiscard]] std::vector sieve1(std::size_t size) { std::vector n(size, false); std::vector r; for (std::size_t i = 2; i < n.size(); ++i) { if (n[i] == false) { r.emplace_back(i); for (std::size_t j = i + i; j < n.size(); j += i) n[j] = true; } } return r; } [[nodiscard]] std::vector sieve2(std::size_t size) { std::vector n(size, false); std::vector r{2}; for (std::size_t i = 3; i < n.size(); i += 2) { if (n[i] == false) { r.emplace_back(i); for (std::size_t j = i + i; j < n.size(); j += i) n[j] = true; } } return r; } [[nodiscard]] std::vector sieve3(std::size_t size) { std::vector n(size, false); std::vector r{2}; std::size_t s = sqrt(n.size()); for (std::size_t i = 4; i <= s; i += 2) n[i] = true; for (std::size_t i = 3; i <= s; i += 2) { if (n[i] == false) { r.emplace_back(i); for (std::size_t j = i * i; j < n.size(); j += i) n[j] = true; } } s += s % 2 ? 2 : 1; for (std::size_t i = s; i < n.size(); i += 2) if (n[i] == false) r.emplace_back(i); return r; }