n/2
이하까지 돌면서 a, b 를 구하고 a, b 가 모두 소수이면 정답 출력시간초과ㅠㅠ
⭐️ 소수 판별을 위해 에라토스의 체 사용
n/2
이하까지 돌면서 a, b 를 구하고 a, b 가 모두 소수이면 정답 출력소수판별을 그냥 for 문으로 써서 시간초과 발생하는 듯
#include <iostream>
using namespace std;
#define MAX 1000000
bool visit[MAX]={0};
void chk() {
for (int i=2; i*i<=MAX; i++) {
if (visit[i]==0) {
for (int j=i*i; j<=MAX; j+=i) {
visit[j]=1;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
chk();
while(1) {
int n;
cin >> n;
if(n==0) break;
bool exist=false;
int a=-1,b=-1;
for(int i=1;i<n/2;i++) {
a=2*i+1;
b=n-a;
if(!visit[a]&&!visit[b]) {
cout << n << " = " << a << " + " << b <<"\n";
exist=true;
break;
}
}
if(!exist) cout << "Goldbach's conjecture is wrong.\n";
}
}