정수론 PS #boj 1929

0ne·2024년 2월 11일

에라토스테네스의 체를 사용한 소수 판별

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

#define FASTIO   cin.tie(0);  cout.tie(0); ios_base::sync_with_stdio(0);

const int MAX = 1000000;
bool check[MAX+1];

int main() {
    FASTIO;
    check[0] = check[1] = true;
    
    for (int i=2; i*i<=MAX; i++) {
        if (check[i] == false) {
            for (int j=i+i; j<=MAX; j+=i) {
                check[j] = true;
            }
        }
    }
    int m, n;
    cin >> m >> n;
    for (int i=m; i<=n; i++) {
        if (check[i] == false) {
            cout << i << '\n';
        }
    }
    return 0;
}
profile
@Hanyang univ(seoul). CSE

0개의 댓글