UVA 10394 - Twin Primes Solution



Solve in  C++:

///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE
///Islamic University,Bangladesh

#include<bits/stdc++.h>
#define m 20000009
using namespace std;
long long int n,ar[m]={0},r[m]={0},i,j,c;
int main()
{
    for(i=0; i<m; i++)
    {
        ar[i]=0;
    }
    ar[0]=1;
    ar[1]=1;
    for(i=4; i<m; i=i+2)
    {
        ar[i]=1;
    }
    for(i=3; i*i<m; i=i+2)
    {
        for(j=i*i; j<m; j=j+2*i)
        {
            ar[j]=1;
        }
    }
    c=1;
    for(long long int i=2; i<m; i++)
    {
        if((ar[i]==0)&&(ar[i+2]==0))
        {
            r[c++]=i;
        }

    }
    while(cin>>n)
    {
        cout<<"("<<r[n]<<", "<<r[n]+2<<")"<<endl;
    }
}

No comments

Most View Post

Recent post

RESTful APIs with CRUD Operations in Laravel 12| (2025)

  RESTful APIs serve as the foundation of modern web development. They follow a set of rules called Representational State Transfer (REST) t...

Powered by Blogger.