-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fd4790
commit 8c6f2d2
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |