Monday, November 13, 2017

Primality Test Codechef Problem Solution

https://www.codechef.com/problems/PRB01

In C++............................

#include<bits/stdc++.h>

using namespace std;


int main()

{
  int t,a;
  cin>>t;
  while(t--)
  {
      cin>>a;
      if(a==2)
      {
          cout<<"yes"<<endl;
      }
      else if(a%2==0)
      {
          cout<<"no"<<endl;
      }
      else
      {
          int s=0;
          for(int i=3;i<=sqrt(a);i=i+2)
          {
              if(a%i==0)
              {
                  cout<<"no"<<endl;
                  s=1;
                  break;
              }
          }
          if(s==0)
          {
              cout<<"yes"<<endl;
          }

      }
  }
  return 0;
}

No comments:

Post a Comment