Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
prime_numbers.cpp File Reference

Get list of prime numbers. More...

#include <iostream>
#include <vector>
Include dependency graph for prime_numbers.cpp:

Functions

std::vector< int > primes (size_t max)
 
int main ()
 

Detailed Description

Get list of prime numbers.

See also
primes_up_to_billion.cpp sieve_of_eratosthenes.cpp

Function Documentation

◆ main()

int main ( void )

main function

34 {
35 std::cout << "Calculate primes up to:\n>> ";
36 int n = 0;
37 std::cin >> n;
38 std::vector<int> ans = primes(n);
39 for (int p : ans) std::cout << p << ' ';
41}
T endl(T... args)
STL namespace.
std::vector< int > primes(size_t max)
Definition prime_numbers.cpp:12
Here is the call graph for this function:

◆ primes()

std::vector< int > primes ( size_t max)

Generate an increasingly large number of primes and store in a list

12 {
14 std::vector<bool> is_not_prime(max + 1, false);
15 for (size_t i = 2; i <= max; i++) {
16 if (!is_not_prime[i]) {
17 res.emplace_back(i);
18 }
19 for (int p : res) {
20 size_t k = i * p;
21 if (k > max) {
22 break;
23 }
24 is_not_prime[k] = true;
25 if (i % p == 0) {
26 break;
27 }
28 }
29 }
30 return res;
31}
double k(double x)
Another test function.
Definition composite_simpson_rule.cpp:117
T emplace_back(T... args)
T max(T... args)
Here is the call graph for this function: