๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/Note

[C++, ํ…œํ”Œ๋ฆฟ] ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ(์—๋ผํ† ์ŠคํŠธ๋„ค์Šค์˜ ์ฒด)

by dkswnkk 2022. 4. 11.

์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ(์—๋ผํ† ์ŠคํŠธ๋„ค์Šค์˜ ์ฒด)

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main() {

    int N = 10000; // 0 ~10000๊นŒ์ง€์˜ ์†Œ์ˆ˜ ๊ตฌํ•˜๊ธฐ
    vector<bool>prime(N + 1, true); // ์ตœ์ข…์ ์œผ๋กœ true์ผ ๊ฒฝ์šฐ ์†Œ์ˆ˜
    
    prime[0] = prime[1] = false;
    for(int i=2; i<=sqrt(N); i++){
        if (prime[i] == true) {
            int k = 2;
            while (i * k <= N) {
                prime[i * k] = false;
                k++;
            }
        }
    }
    
}

 

๋Œ“๊ธ€