prime numbers Algorithm
In abstract algebra, objects that behave in a generalized manner like prime numbers include prime components and prime ideals. A prime number (or a prime) is a natural number greater than 1 that is not a merchandise of two smaller natural numbers. method that are restricted to specific number forms include Pépin's test for Fermat numbers (1877), Proth's theorem (c. 1878), the Lucas – Lehmer primality test (originated 1856), and the generalized Lucas primality test.
#include <iostream>
#include <vector>
std::vector<int> primes(int max) {
max++;
std::vector<int> res;
std::vector<bool> numbers(max, false);
for (int i = 2; i < max; i++) {
if (!numbers[i]) {
for (int j = i; j < max; j += i)
numbers[j] = true;
res.push_back(i);
}
}
return res;
}
int main() {
std::cout << "Calculate primes up to:\n>> ";
int n;
std::cin >> n;
std::vector<int> ans = primes(n);
for (int i = 0; i < ans.size(); i++)
std::cout << ans[i] << ' ';
std::cout << std::endl;
}