Problem:
Twin Prime
Prime numbers are widely applied for cryptographic and communication technology.
A twin prime is a prime number that differs from another prime number by 2.
For example, (5, 7) and (11, 13) are twin prime pairs.
In this problem, we call the greater number of a twin prime "size of the twin prime."
Your task is to create a program which reads an integer
n
and prints a twin prime which has the maximum size among twin primes less than or equals to
n
You may assume that 5 ≤
n
≤ 10000.
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing one zero. Each dataset is formatted as follows:
n
(integer)
Output
For each dataset, print the twin prime
p
and
q
(
p
<
q
).
p
and
q
should be separated by a single space.
Sample Input
12
100
200
300
0
Output for the Sample Input
5 7
71 73
197 199
281 283

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

bool is_prime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i <= sqrt(n); i++){
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

int main() {
    int n;
    while(true) {
        cin >> n;
        if (n==0) break;
        for(int i=n; i>=3; i--) {
            if (is_prime(i) && is_prime(i-2)) {
                cout << i-2 << " " << i << endl;
                break;
            }
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3124
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

bool is_prime(int n) {
    if (n < 2) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    int n;
    while (true) {
        cin >> n;
        if (n == 0) break;
        for (int i = n; i >= 3; i--) {
            if (is_prime(i) && is_prime(i - 2)) {
                cout << i - 2 << " " << i << endl;
                break;
            }
        }
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3336
final_rank: 17

code_snippet: #include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define si size()
using namespace std;

bool is_prime(int n) {
    if (n == 1) {return 0;} if (n == 2) {return 1;} if (n % 2 == 0) {return 0;}
    for (int i = 3; i <= sqrt(n); i++) {
        if (n % i == 0) {return 0;}
    } return 1;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    int n;
    while(true) {
        cin >> n;
        if (n==0) break;
        for(int i=n; i>=3; i--) {
            if (is_prime(i) && is_prime(i-2)) {
                cout << i-2 << " " << i << endl;
                break;
            }
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3136
final_rank: 13

code_snippet: #include<bits/stdc++.h>
#define N ((int)  (1e4+8))
using namespace std;
bool mark[N];




int main()
{
   int n;

    while(cin>>n && n!=0)
    {


for(int i=1;i<=n;i++)
{
    mark[i]=0;
}
     for(int i=3;i*i<=n;i+=2)
    {
        if(mark[i]==0)
        {
            for(int j=i*i;j<=n;j+=i+i)
            {
                mark[j]=1;
            }
        }
    }

 vector<int>prime;
  prime.push_back(2);



    for(int i=3;i<=n;i+=2)
    {
        if(mark[i]==0)
        {

            prime.push_back(i);


        }
    }
int k,x,y,j;
      //for(k=0;k<prime.size();k++)
    //{
      j=prime.size()-1  ;
    do
    {


         if((prime[j]-prime[j-1])==2)
         {
              x=prime[j];
              y=prime[j-1];
              break;
         }

    }
while( j--);


    //}
cout<<y<<" "<<x<<endl;
prime.clear();
}
}
accuracy: 1
time_ms: 40
memory_kb: 3468
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#define ll long long
using namespace std;

ll p1=0,p2=0;

bool prima(ll x) {
    for(ll i = 2; i*i <= x; i++) {
        if(x%i==0) return false;
    }
    return true;
}

int main() {
  ll n; cin >> n;
  while(n != 0) {
      for(ll i=n; i>2; i--) {
          if(prima(i)) {
              p1 = p2;
              p2 = i;
              if(p1-p2==2) {
                cout << p2 << " " << p1 << endl;
                break;
              }
          }
      }
      cin >> n;
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3120
final_rank: 14

code_snippet: #include <iostream>
#include <vector>
using namespace std;

// Function to generate all primes up to max_n using the Sieve of Eratosthenes
vector<bool> generatePrimes(int max_n) {
    vector<bool> is_prime(max_n + 1, true);
    is_prime[0] = is_prime[1] = false; // 0 and 1 are not primes
    for (int i = 2; i * i <= max_n; ++i) {
        if (is_prime[i]) {
            for (int j = i * i; j <= max_n; j += i) {
                is_prime[j] = false;
            }
        }
    }
    return is_prime;
}

int main() {
    const int MAX_N = 10000;
    vector<bool> is_prime = generatePrimes(MAX_N);

    int n;
    while (cin >> n && n != 0) {
        int max_p = 0, max_q = 0;
        for (int i = 2; i <= n - 2; ++i) {
            if (is_prime[i] && is_prime[i + 2]) {
                max_p = i;
                max_q = i + 2;
            }
        }
        if (max_p && max_q) {
            cout << max_p << " " << max_q << endl;
        }
    }

    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3072
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n;

/*
    untuk mengecek apakah nbilangan prima
    kita coba bagi dengan 2, 3, 4, .. akar(n)
    jika habis dibagi, maka dapat disimpulkan n bukan prima

    jika sampai selesai tidak ada yg habis dibagi, maka n prima

*/


bool is_prime(int n){
    for (int i = 2; i*i <= n; i++){
        if ( n % i == 0 ){
            return false;
        }
    }
    return true;
}

int main (){
    while(true) { // true == sebuah nilai, bakal diulang2 terus
        cin >> n;
        if (n==0) break;
        for (int i=n; i>=0; i--){ // 5 <= n <= 10000
            if (is_prime(i) && is_prime(i-2)){
                cout << i-2 << " " << i << endl;
                break;
            }
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3052
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    vector<bool> tb(100001, true);
    tb[0] = tb[1] = false;
    for(int i = 2; i <= 100000; i++){
        if(!tb[i])continue;
        for(int j = 2 * i; j <= 100000; j += i){
            tb[j] = false;
        }
    }
    int n;
    while(cin >> n, n){
        int ans1, ans2;
        for(int j = 3; j + 2 <= n; j += 2){
            if(tb[j] && tb[j + 2]){
                ans1 = j;
                ans2 = j + 2;
            }
        }
        cout << ans1 << " " << ans2 << '\n';
    }
}
accuracy: 1
time_ms: 40
memory_kb: 3064
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
int n(1);
int prime(int a) {
	if(a%10 == 1 || a%10 == 3 || a%10 == 5 || a%10 == 7 || a%10 == 9 ){
		for(int i=2; i<=a/2; i++) {
			if(a%i == 0 || (a-2)%i == 0) {
				return prime(a-1);
			}
		}
	    return a; 
	}
	else {
		return prime(a-1);
	}
}
int main() {
	while(n != 0) {
	    cin >> n;
	    if(n != 0) {
	    	cout << prime(n) - 2 << " " << prime(n) << endl;
		}	
	}
}
accuracy: 1
time_ms: 110
memory_kb: 2980
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;

bool cek(int x){
	bool prime = true;
	for(int i=2; i*i<=x; i++){
		if(x%i == 0){
			prime = false;
			break;
		}	
	}
	if(x == 1){
		return false;
	} else if(x == 2){
		return true;
	} else if(prime == true){
		return true;
	} else if(prime == false){
		return false;
	}
}
int n; 

int main(){
	while(cin >> n && n!=0){
		for(int i=n; i>=1; i--){
			if(cek(n) && cek(n-2)){
				cout << n-2 << " " << n << endl;
				break;
			} else {
				n--;
			}
		}
	}
	
}
accuracy: 1
time_ms: 10
memory_kb: 3068
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(void){
    while(1){
        int n;
        cin>>n;
        if(n==0){
            return 0;
        }
        vector<bool> is_prime;
        is_prime.resize(n+1,true);
        is_prime[0]=false;
        is_prime[1]=false;
        for(int i=2;i<=n;i++){
            if(is_prime[i]==true){
                for(int j=2;i*j<=n;j++){
                    is_prime[i*j]=false;
                }
            }
        }
        int m=-1;
        for(int i=0;i<=n;i++){
            if(i+2<=n && (is_prime[i] && is_prime[i+2])){
                m=max(m,i+2);
            }
        }
        cout<<m-2<<" "<<m<<endl;
    }
}
accuracy: 1
time_ms: 290
memory_kb: 3096
final_rank: 16

code_snippet: #include <iostream>
#include <math.h>
#include <vector>

bool IsPrime(int num){
    if(num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false;

    double sqrtNum = sqrt(num);
    for(int i = 3; i <= sqrtNum; i += 2){
        if(num % i == 0){
            return false;
        }
    }

    return true;

} 

bool TwinPrime(int num){
    int count = 0;
    int twiprime1, twiprime2;
    std::vector<int> prime;

    prime.resize(num);

    for(int i = 1; i <=  num; i++){
        if(IsPrime(i) == 1){
            prime[count] = i;
            count++;
        }
    }

    for(int i = 0; i < count; i++){
        if(prime[i+1] - prime[i] == 2){
            twiprime1 = prime[i];
            twiprime2 = prime[i+1];
        }
    }

    std::cout << twiprime1 << " " << twiprime2 << std::endl;
}

int main(){


    int n;
    //std::vector<int> v;
    //v.resize(10000);

    for(int i = 0; i <= 10000; i++){
        std::cin >> n;
        if(n == 0){return 0;}
        TwinPrime(n);
    }

    return 0;

}
accuracy: 1
time_ms: 780
memory_kb: 3104
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
int is_prime(unsigned int a){
    if(a < 2)
        return 0;
    for(int i = 2; i * i <= a; i++){
        if(a % i == 0)
            return 0;
    }
    return 1;
}

int main(){
    int n = 0;
    unsigned int a;
    std::vector<unsigned int> v;
    v.resize(10000);

    for(int i = 0; i < 10000; i++){
        std::cin >> v[i];
        if(v[i] == 0){
            n = i;
            break;
        }
    }
    for(int i = 0; i < n; i++){
        a = v[i];
        for(unsigned int j = a; j > 4; j--){
            if( is_prime(j) ){
                if(is_prime(j - 2)){
                    std::cout << j - 2 << " " << j << std::endl;
                    break;
                }
            }
        }
        
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3036
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define d double

bool prime(int n){
    int m = sqrt(n);
    for(int i=2; i<=m; i++){
        if(n%i==0){
            return false;
        }
    }
    return true;
}

int main() {
    int n;
    while(cin >> n) {
        if (n==0) {
            break;
        }
        for(int i=n; i>=1; i--){
            if(prime(i)==true && prime(i-2)==true){
                cout << i-2 << " " << i << endl;
                break;
            }
        }
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3080
final_rank: 8

code_snippet: #include<iostream>
using namespace std;
int n;
bool isP(int x){
  if(x==0||x==1)return false;
  for(int i=2;i*i<=x;i++){
    if(x%i==0)return false;
  }
  return true;
}
int main(){
  while(cin>>n&&n){
    for(int i=n;i-2>=1;i--){
      if(isP(i)&&isP(i-2)){
	cout<<i-2<<' '<<i<<endl;
	break;
      }
    }
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3088
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
int prime(int z)
{
    for(int i=2;i<z;i++) if(z%i==0) return 0;
    return 1;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    while(1)
    {
        int n,i;
        cin >> n;
        if(n==0) break;
        if(n%2 == 0)n-=1;
        for(i=n;i>=2;i-=2)
        {
            if(prime(i) && prime(i-2))
            {
                cout << i-2 << " " << i << "\n";
                break;
            }
        }
    }

    return 0;
}
accuracy: 1
time_ms: 700
memory_kb: 3136
final_rank: 19

code_snippet: #include <iostream>
#include <cmath>

bool isPrime(int n)
{
	int ub = (int)std::sqrt((float)n);
	for (int i=2; i<=ub; i++) {
		if (n%i == 0) return false;
	}
	return true;
}

int main()
{
	int n;
	while (std::cin >> n) {
		for (int p=n; p>=2; p--) {
			if (isPrime(p) && isPrime(p-2)) { std::cout << p-2 << " " << p << std::endl; break; }
		}
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 2992
final_rank: 1

code_snippet: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

vector<int> get_prime(int n){
    if(n <= 1) return vector<int>();
    vector<bool> is_prime(n+1, true);
    vector<int> prime;
    is_prime[0] = is_prime[1] = 0;
    for (int i = 2; i <= n; ++i) {
        if(is_prime[i]) prime.emplace_back(i);
        for (auto &&j : prime){
            if(i*j > n) break;
            is_prime[i*j] = false;
            if(i % j == 0) break;
        }
    }
    return prime;
}

int main() {
    auto v = get_prime(10000);
    vector<int> ans;
    for (int i = 0; i+1 < v.size(); ++i) {
        if(v[i+1] - v[i] == 2){
            ans.emplace_back(v[i]+2);
        }
    }
    int n;
    while(cin >> n, n){
        auto x = *--upper_bound(ans.begin(),ans.end(), n);
        printf("%d %d\n", x-2, x);
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3216
final_rank: 15

code_snippet: #include <iostream>
#include <cmath>
using namespace std;


bool isprime(int n);

int main(void){
  int n;
  while(1){
    cin>>n;
    if(n==0)break;

    for(int j=n;j>0;j--){
      if(isprime(j) && isprime(j-2)){
        cout<<j-2<<" "<<j<<endl;
        break;
      }
    }
  }
  return 0;
}

bool isprime(int n){
  for(int i=2;i<min((int)sqrt(n)+1,n);i++){
    if(n%i==0){
      return false;
    }
  }
  return true;
}
accuracy: 1
time_ms: 30
memory_kb: 3016
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    std::deque<int> arr(10001,1);
    std::deque<int> prime;
    arr[0]=0;
    arr[1]=1;
    for (int i=2;i*i<10000;i++) {
        if (arr[i]==1) {
            for (int j=i*i;j<10000;j+=i) {
                arr[j]=0;
            }
        }
    }
    for (int k=2;k<=10000;k++) {
        if (arr[k]==1) {
            prime.emplace_back(k);
        }
    }
    int n;
    while (cin>>n) {
        if (n==0) {
            break;
        } else {
            auto itr=upper_bound(prime.begin(),prime.end(),n);
            int dis=distance(prime.begin(),itr);
            dis--;
            for (int l=dis;l>0;l--) {
               if (prime[l]-prime[l-1]==2) {
                    cout<<prime[l-1]<<" "<<prime[l]<<endl;
                    break;
                }
            }
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3128
final_rank: 12