소수찾기 - 백준(1929) [99클럽 코테 TIL]

동키·2025년 3월 31일

알고리즘

목록 보기
1/10


1일차 문제
소수찾기 - 백준(1929)

범위가 (1 ≤ M ≤ N ≤ 1,000,000) 이기 때문에
에라토네스의 체를 사용해서 해결했습니다.
에라토네스의 체

소스코드

https://www.acmicpc.net/source/share/399e17ab727a4221925920d1b585242c

#include<bits/stdc++.h>
using namespace std;
int n, m;

bool solve(int a) {
    if(a <= 1) return 0;
    if(a == 2) return 1;
    if(a % 2 == 0) return 0;
    for(int i = 2; i * i <= a; i++) {
        if(a % i == 0) return 0;
    }
    return 1;
} 
int main() {
    cin >> n >> m;
    for(int i = n; i <= m; i++) {
        if(solve(i)) cout << i << '\n';
    }
    return 0;
}
profile
오늘 하루도 화이팅

0개의 댓글