UVA-10394 Twin Prime

#include <bits/stdc++.h>
using namespace std;
#define n 20000005
long long int twin[n];
bool a[n];

void sieve()
{
    long long int i,j,k=1;
    a[0]=a[1]=1;
    for(i=4;i<n;i=i+2)
    {
        a[i]=1;
    }
    for(i=3;i<sqrt(n);i=i+2)
    {
        for(j=i*i;j<n;j=j+2*i)
        {
            a[j]=1;
        }
    }
    for(i=2;i<n;i++)
    {
        if(a[i]==0 && a[i+2]==0)
        {
            twin[k]=i;
            k++;
        }
    }
}
int main()
{
    long long int m;
    sieve();
    while(cin>>m)
    {
    cout<<"("<<twin[m]<<", "<<twin[m]+2<<")"<<endl;
    }
    return 0;
}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment