Prime number only can be devided by 1 and itself. ( only two 약수 )

1 is not a prime number or not a composition number.

 

#include <iostream>
#include <vector>
using namespace std;

vector<int> era(int mx_n){
	vector<int> v;
	vector<int> che(mx_n+1);
	
	for (int i=2; i<=mx_n; i++){
		if (che[i]) continue;
		for (int j=2*i; j<=mx_n; j += i) {
			che[j] = 1;
		}
	}
	
	for(int i = 2; i <= mx_n; i++) {
		if(che[i] == 0) v.push_back(i);		
	} 
	return v;
}

int main(){
	vector<int> result;
	
	result = era(100);		
	for (auto a : result) cout << a << " ";
	
	return 0;
}
반응형

'Prog&Algol' 카테고리의 다른 글

BJ | 2309 | 일곱 난쟁이 | Combination  (0) 2022.03.22
C++ | lower_bound & upper_bound  (0) 2022.03.22
Math | Permutation & Combination  (0) 2022.03.21
Algo | C++ | priority Queue  (0) 2022.03.20
C++ | Range-based for loops  (0) 2022.03.20

+ Recent posts