top of page

CCC 2019 S2: Pretty Average Primes

For this problem, we can use a simple for-loop to generate values of a and b.Set a=i and b=2n−i. Loop it through the integers from 2 to 2n. Check if they are both prime and that 2n is equal to a+b. Recall that an integer is prime if it is greater than 1 and cannot be formed by multiplying two smaller natural numbers.

  1. Loop i from 2 to sqrt(n)

  2. Exclude even numbers.

#include <iostream> #include <cmath> using namespace std; bool isprime(int n){ if(n < 2){ return false; } if(n==2){ return true; } if(n%2 == 0){ return false; } int top = sqrt(n) +1; for(int i = 3; i <= top; i+=2){ if(n%i == 0){ return false; } } return true; } void getPrimeAverage(int N){ for(int i = 2; i <=N; i++){ if(isprime(i)){ if(isprime(2*N -i)){ cout<<i<<" "<<(2*N-i)<<endl; return; } } } return; } int main() { int T; cin >> T; for(int i = 0; i < T; i++){ int N; cin >> N; getPrimeAverage(N); } return 0; }

Recent Posts

See All

CCC '24 J5 - Harvest Waterloo

#include<iostream> #include <vector> #include <algorithm> #include <cmath> #include <stack> using namespace std; int main() { int r, c, sr, sc; cin >> r; cin >> c; int p[r][c]; bool v

CCC '24 J4 - Troublesome Keys

#include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> #include <cmath> using namespace std; int main() { string ps; string ds; cin >> ps; cin >> ds;

CCC '22 J5 - Square Pool

#include<iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; bool rowcom(pair<int, int> a, pair<int, int> b){ return a.first < b.first; } bool colcom(pair<int,

Comments


bottom of page