문제

풀이
제곱근 판별법을 통해 주어진 수 n과 크거나 같은 소수 중 가장 작은 소수를 찾아 출력하였다.
#include <iostream>
#include <algorithm>
using namespace std;
bool isPrime(unsigned int n) {
for (unsigned int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
unsigned int num;
cin >> num;
if (num < 2) num = 2;
else {
while (true) {
if (isPrime(num) == true) break;
num++;
}
}
cout << num << "\n";
}
return 0;
}
'백준 > 약수, 배수와 소수' 카테고리의 다른 글
4948번: 베르트랑 공준 [C++] (0) | 2023.03.29 |
---|---|
1929번: 소수 구하기 [C++] (0) | 2023.03.29 |
2485번: 가로수 [C++] (0) | 2023.03.29 |
1735번: 분수 합 [C++] (0) | 2023.03.29 |
13241번: 최소공배수 [C++] (0) | 2023.03.29 |