A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/de/d9b/prime__numbers_8cpp_source.html below:

TheAlgorithms/C++: math/prime_numbers.cpp Source File

Go to the documentation of this file. 1 6#include <iostream> 7#include <vector> 8 12

std::vector<int>

primes

(

size_t

max) {

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

}

32 34int main

() {

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

}

primes

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

Definition prime_numbers.cpp:12 main

int main()

Definition prime_numbers.cpp:34

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4