Problem Link
For any integer n (n ≥ 4) there exist two prime numbers p1 and p2 such that p1 + p2 = n. In a problem we might need to find the number of essentially different pairs (p1, p2), satisfying the condition in the conjecture for a given even number n (4 ≤ n ≤ 2 15). (The word ‘essentially’ means that for each pair (p1, p2) we have p1 ≤p2.)
For example, for n = 10 we have two such pairs: 10 = 5 + 5 and 10 = 3 + 7.
Thus my solution in C++ :
For any integer n (n ≥ 4) there exist two prime numbers p1 and p2 such that p1 + p2 = n. In a problem we might need to find the number of essentially different pairs (p1, p2), satisfying the condition in the conjecture for a given even number n (4 ≤ n ≤ 2 15). (The word ‘essentially’ means that for each pair (p1, p2) we have p1 ≤p2.)
For example, for n = 10 we have two such pairs: 10 = 5 + 5 and 10 = 3 + 7.
Thus my solution in C++ :
- #include<bits/stdc++.h>
- using namespace std;
- #define MAX 10000000 ///~10^7
- bool prime[MAX];
- vector <int> p;
- void sieve()
- {
- memset(prime,0,sizeof(prime));
- prime[0]=prime[1]=1;
- for(int i = 4; i < MAX; i += 2)
- {
- prime[i] = 1;
- }
- for(int i = 3; i*i < MAX; i += 2)
- {
- if(!prime[i])
- {
- for(int j = i*i; j < MAX; j += i)
- prime[j] = 1; /// not prime
- }
- }
- for(int i = 2; i < MAX; i++)
- if(!prime[i])
- p.push_back(i);
- }
- int FindSol(int n)
- {
- int res = 0;
- for(int i = 2; i <= n/2; i++)
- if(!prime[i] && !prime[n-i])
- res++;
- return res;
- }
- int main()
- {
- int n;
- sieve();
- while(cin>>n && n)
- cout<<FindSol(n)<<endl;
- return 0;
- }
No comments:
Post a Comment