UVA 1180 Perfect Numbers




Hints : You need to check  p and (2^p -1) is prime or not . ( 2^p -1) is  an odd number. So also check ( 2^p -1) is even or not before prime checking as  input is large enough upto 2^33.


#include <bits/stdc++.h>
using namespace std;
long long int isprime(long long int p)
{
    long long int i,j,k;
    if(p==1)
    {
        return 0;
    }
    else if (p==2)
    {
        return 1;
    }
    else if (p%2==0)
    {
        return 0;
    }
    else
    {
        for(i=3;i<=sqrt(p);i=i+2)
        {
            if(p%i==0)
            {
                return 0;

            }
        }
    }
    return 1;
}


int main()
{
    long long int t,y,pr,yj,j;
    double pd;
    //freopen("1180in.txt","r",stdin);
    //freopen("1180out.txt","w",stdout);
    cin>>t;
    for(y=1;y<=t;y++)
    {
        scanf("%lld,",&pr);


        j=pow((double)2,pr)-1;
        if(isprime(pr) && isprime(j))
        {
            cout<<"Yes"<<endl;
        }
        else
        {
            cout<<"No"<<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