Skip to content

Commit

Permalink
Create 543 - Goldbach's Conjecture
Browse files Browse the repository at this point in the history
  • Loading branch information
ddthanhdat committed Jun 9, 2015
1 parent 7fd4790 commit 8c6f2d2
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions 543 - Goldbach's Conjecture
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
543 - Goldbach's Conjecture
// Thanh Dat
Note:
- n = a + b, where a and b are odd primes.
a và b là 2 số nguyên tố lẻ.

Bài này do số nhỏ nên ta lợi dụng chỉ số mảng, sàng nguyên tố.
Code:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define ll long long
#define maxN 1000000
using namespace std;
bool isPrime[maxN+5];
void erathosthenes(){
for(ll i=2;i<=maxN;i++)
isPrime[i]=true;
for(ll i=2;i<=maxN;i++)
if(isPrime[i]){
for(ll j=i;i*j<=maxN;j++)
isPrime[i*j]=false;
}
isPrime[2] = isPrime[1] = 0;
}
int main(){
erathosthenes();
ll n;
while(cin >> n){
if(!n) break;
ll a,b;
for(ll i=3;i<=n;i++){
if(isPrime[i] && isPrime[n-i]){
a=i; b=n-i;
break;
}
}
if(n!=a+b) cout << "Goldbach's conjecture is wrong." <<endl;
else
cout << n << " = " << a << " + " << b << endl;
}

}

0 comments on commit 8c6f2d2

Please sign in to comment.