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

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.