공부/백준

4948번: 베르트랑 공준(백준 C++)

상연 2020. 10. 15. 17:28

목차

4948번: 베르트랑 공준 링크

코드

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

bool judge_prime(int num){
    if(num == 1) return 0;
    for(int i = 2; i <= sqrt(num); i++){
        if(num % i == 0) return 0;
    }
    return 1;
}

int main() {
    int n;
    while(1){
        cin >> n;
        int cnt = 0;

        if(n == 0) break;

        for(int i = n+1; i <= 2 * n; i++){
            if(judge_prime(i)) cnt += 1;}
        cout << cnt << "\n";}
}

사견

특별히 할 말이 없는 문제이다.
이전에 몇 번 풀었던 소수 문제이기 때문.
에라토스테네스의 체를 활용하여 풀면 되는 간단한 문제이다.

2581번: 소수 문제풀이
이전에 내가 풀었던 이 포스팅을 보면 이해에 조금 도움이 될 수 있다.

에라토스테네스의 체 위키백과