TheAlgorithms/C++ 1.0.0
All the 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:

Go to the source code of this file.

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

Definition in file prime_numbers.cpp.

Function Documentation

◆ main()

int main ( void )

main function

Definition at line 34 of file prime_numbers.cpp.

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 << ' ';
40 std::cout << std::endl;
41}
std::vector< int > primes(size_t max)

◆ primes()

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

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

Definition at line 12 of file prime_numbers.cpp.

12 {
13 std::vector<int> res;
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.