Wednesday, October 10, 2018

Solution of URI 1221::Fast Prime Number

Before seeing the solution make sure that you have tried enough. Don’t copy the whole code, just find out the logic. If you feel any problemyou will inform me by comment.




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

int main()
{
    int N,check,X;

    cin>>N;
    while(N--)
    {
        cin>>X;
        check=1;

        if(X==2)
        {

        }
        else if(X%2==0)
        {
            check=0;

        }

        else
        {
            for(int i=3; i<=sqrt(X); i=i+2)
            {
                if(X%i==0)
                {
                    check=0;
                    break;
                }
            }
        }

        if(check==1)
        {
            cout<<"Prime"<<endl;
        }

        else
        {
            cout<<"Not Prime"<<endl;
        }
    }

    return 0;
}

No comments:

Post a Comment