🖥️ CS/Baekjoon Algorithms

#1929번 소수 구하기 (c++)

한국의 메타몽 2020. 2. 17. 02:36
#include <iostream>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int a,b =0;
    bool check[1000001] = {false,};
    cin >> a >> b;
    
    check[1] = {true};
    
    for(int i=2; i<=b; i++)
    {
        if(check[i]==false)
        {
            if(i>=a&&i<=b)
                cout << i << "\n";
            for(int j=i*2; j<=b; j+=i)
            {
                check[j]=true;
            }
        }
    }
    return 0;
}

에라스토네스의 채를 이용하면

쉽게 풀리는 문제다.